====== Writing Classes Practice ======
These are designed to be done with an actual computer. You would need to write the class from scratch and use the Tester class to see how your work is progressing. Usually when you write a class for a Quiz, Test, or Exam, the task will be simpler (since you have no computer). You learn a lot by finding and fixing mistakes yourself on a computer, which is why this is such great practice.
===== Clock =====
- The Clock class should have three fields: hours minutes and seconds
- There should be a constructor that takes three values and initializes the fields
- There should be a ''toString'' method so that it returns a string with the current time. (For example "12:52:34"
- There should be a method called ''tick'' that advances the seconds by one and sees if the minutes and hours need to be changed. For example, if the current time is 11:59:59, after the method is called, the time should become "12:00:00";
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ClockTester extends JPanel implements ActionListener
{
public static int WIDTH=800;
public static int HEIGHT=600;
private Font titleFont, regularFont, clockFont;
private int x;
private Timer motion, pulse;
private Clock clock;
public ClockTester()
{
//initialize variables here...
titleFont = new Font("Roman", Font.BOLD, 18);
clockFont = new Font("Roman", Font.BOLD, 72);
regularFont = new Font("Helvetica", Font.PLAIN, 12);
x=0;
motion = new Timer(2, this); //1000=1 seconds
pulse = new Timer(1000, this); //1000=1 seconds
clock =new Clock(11, 59, 50);
motion.start();
pulse.start();
}
public static void main(String[] args) {
ClockTester app= new ClockTester();
JFrame window = new JFrame("Clock Tester");
window.setSize(WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(app);
window.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(),getHeight());
g.setColor(Color.BLUE);
g.setFont(titleFont);
g.drawString("Clock Tester", 20, 20);
g.setColor(Color.BLACK);
g.setFont(regularFont);
//g.drawString("pulse = "+x, 20, 40);
g.fillOval(x, 200, 10, 10);
g.setColor(Color.RED);
g.setFont(clockFont);
g.drawString(clock.toString(), getWidth()/3, getHeight()/2);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==motion){
x=(x+1)%WIDTH;
} else {
// call the tick method
clock.tick();
}
repaint();
}
}
===== Cash Register =====
Use the following tester class to figure out how to make your own ''CashRegister.java'' class file.
Hints:
- You probably need to keep track of the total of the purchase, and how much the customer has already paid,
- You need a constructor to initialize the fields.
- You need a ''recordPurchase'' method that added to the amount the customer owes
- You need an ''enterPayment'' method that records how much the customer has paid
- You need a method ''giveChange'', that return the change and adjusts the amount due and clears the amount the customer has paid.
/**
A class to test the CashRegister class.
*/
public class CashRegisterTester
{ has paid
public static void main(String[] args)
{
CashRegister register = new CashRegister();
register.recordPurchase(29.50);
register.recordPurchase(9.25);
register.enterPayment(50);
double change = register.giveChange();
System.out.println(change);
System.out.println("Expected: 11.25");
}
}
===== Song =====
the SongTester class that will test your ''Song'' class. It should be able to keep track of the song's title, artist, rank, time, and number of plays, and it should be able to show and change any of these fields. Finally, overwrite the toString() method so we can print out all the song information
import java.util.Scanner;
public class SongTester {
/**
* @param args
*/
public static void main(String[] args)
{
Song song=new Song();
Scanner keyboard=new Scanner(System.in);
String input="";
while (input.indexOf("q")<0)
{
processCommand(keyboard, song, input);
System.out.println("\nCurrently: "+song+"\n");
System.out.print("Command (Title, Artist, Rank, Time, Plays, Quit): ");
input=keyboard.nextLine();
input=input.toLowerCase();
}
System.out.println("\nThanks.. we exit with the song being:\n"+song);
}
public static void processCommand(Scanner keyboard, Song song, String input){
if (input.equals("title"))
{
System.out.print("Currently: "+song.getTitle()+"\nNew Title: ");
song.setTitle(keyboard.nextLine());
} else if (input.equals("artist"))
{
System.out.print("Currently: "+song.getArtist()+"\nNew artist name: ");
song.setArtist(keyboard.nextLine());
} else if (input.equals("rank"))
{
System.out.print("Currently: "+song.getRank()+"\nNew rank: ");
int newRank=Integer.parseInt(keyboard.nextLine());
song.setRank(newRank);
} else if (input.equals("time"))
{
System.out.print("Currently: "+song.getTime()+"\nNew time: ");
double newTime=Double.parseDouble(keyboard.nextLine());
song.setTime(newTime);
} else if (input.equals("plays"))
{
System.out.print("Currently: "+song.getPlayCount()+"\nNew number of plays: ");
int newPlays=Integer.parseInt(keyboard.nextLine());
song.setPlayCount(newPlays);
}
}
}