User Tools

Site Tools


writing_classes_practice

This is an old revision of the document!


Writing Classes Practice

Clock

  1. The Clock class should have three fields: hours minutes and seconds
  2. There should be a constructor that takes three values and initialize the fields
  3. There should be a toString so that it returns a string with the current time. (For example “12:52:34”
  4. 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”;
ClockTester.java
import

Cash Register

Use the following tester class to figure out how to make your own CashRegister.java class file.

Hints:

  1. You probably need to keep track of the total of the purchase, and how much the customer has already paid,
  2. You need a constructor to initialize the fields.
  3. You need a recordPurchase method that added to the amount the customer owes
  4. You need an enterPayment method that records how much the customer has paid
  5. You need a method giveChange, that return the change and adjusts the amount due and clears the amount the customer has paid.
CashRegisterTest.java
/**
   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

SongTester.java
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);				
  	    }
 
	}
}
writing_classes_practice.1664643553.txt.gz · Last modified: 2022/10/01 12:59 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki