System.out.print("Your vote for president is ");
switch(myVoteForPresident) {
case 0:
System.out.println("no one");
break;
case 1:
System.out.println(nameCandidatePresident1);
break;
case 2:
System.out.println(nameCandidatePresident2);
break;
}
System.out.print("Your vote for president is ");
switch(myVoteForVicePresident) {
case 0:
System.out.println("no one");
break;
case 1:
System.out.println(nameCandidateVicePresident1);
break;
case 2:
System.out.println(nameCandidateVicePresident2);
break;
}
System.out.println("Type yes if you are happy with your vote");
Scanner keyboard = new Scanner(System.in);
return keyboard.next().equals("yes");
}
public void getAndConfirmVotes(){
boolean voteFinished = false;
while(!voteFinished){
getVotes();
voteFinished = confirmVotes();
}
recordVotes();
}
public static void main(String[] args) {
setCandidatesPresident("Annie", "Bob");
setCandidatesVicePresident("John", "Susan");
resetVotes();
boolean moreVotes = true;
while(moreVotes){
VoteRecorder aVoter = new VoteRecorder();
aVoter.getAndConfirmVotes();
System.out.println("Type yes if there is another voter");
Scanner keyboard = new Scanner(System.in);
moreVotes = keyboard.next().equals("yes");
}
System.out.println(currentVotePresident());
System.out.println(currentVoteVicePresident());
}
}

CSJA1DP/203/0/2016
13
Question 4 (Chapter 6 Programming Projects 15 p516)
Notes
:
This project creates a simple little applet ties together the material on applets in this chapter.
A
discussion on how Java handles time could be introduced here, but is not critical to the operation of
this applet
File: GuessApplet.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuessApplet extends javax.swing.JApplet
implements ActionListener {
private long secretNumber;
private boolean isEven;
private JButton oddGuessButton;
private JButton evenGuessButton;
private JLabel correctLabel;
private JLabel wrongLabel;
private JLabel answerLabel;
private Container contentPane;
public void init() {
secretNumber = java.util.Calendar.getInstance().getTimeInMillis()%100;
isEven = secretNumber%2 == 0;
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
//Program button:
oddGuessButton = new JButton("Odd");
oddGuessButton.addActionListener(this);
evenGuessButton = new JButton("Even");
evenGuessButton.addActionListener(this);
//Program label:
correctLabel = new JLabel("Congratulations, you are correct!");
wrongLabel = new JLabel("Sorry, you are wrong.");
answerLabel = new JLabel("The secret number was: " + secretNumber);
correctLabel.setVisible(false);
wrongLabel.setVisible(false);
answerLabel.setVisible(false);
//Add button:
contentPane.setLayout(new FlowLayout());
contentPane.add(evenGuessButton);
contentPane.add(oddGuessButton);

14
//Add label
contentPane.add(correctLabel);
contentPane.add(wrongLabel);
contentPane.add(answerLabel);
}
public void actionPerformed(ActionEvent e) {
contentPane.setBackground(Color.PINK);
if((e.getActionCommand().equals("Even")
&& isEven)
||
(e.getActionCommand().equals("Odd")
&& !isEven))
correctLabel.setVisible(true);
else
wrongLabel.setVisible(true);
answerLabel.setVisible(true);
oddGuessButton.setVisible(false);
evenGuessButton.setVisible(false);
}
}
Question 5 (Chapter 7 Programming Projects 1 p611)
Notes:
This project is a bit challenging to get the loop conditions right.


You've reached the end of your free preview.
Want to read all 20 pages?
- Spring '16
- Java Programming, Software engineering, ........., Fahrenheit, Celsius