Software and Programming I
Lab 7: Construction of a Simulated Cash Register and a Student Class
1
SP1-Lab7-20.pdf Tobi Brodie (Tobi@dcs.bbk.ac.uk) 27 February 2020
Software and Programming I Lab 7: Construction of a Simulated Cash - - PowerPoint PPT Presentation
Software and Programming I Lab 7: Construction of a Simulated Cash Register and a Student Class SP1-Lab7-20.pdf 1 27 February 2020 Tobi Brodie (Tobi@dcs.bbk.ac.uk) Coursework Plagiarism Plagiarism is claiming the work of others as your
1
SP1-Lab7-20.pdf Tobi Brodie (Tobi@dcs.bbk.ac.uk) 27 February 2020
2
3
4
5
6
7
8
9
10
public class CashRegisterTest { public static void main(String[] args) { // create an instance (register 1) of the CashRegister class CashRegister r1 = new CashRegister(); // add items to register 1 r1.addItem(2.95); r1.addItem(1.99); // use the CashRegister instance method getCount() System.out.println(r1.getCount()); System.out.println((r1.getCount() == 2) ? "OK" : "FAIL"); // use the CashRegister instance method getTotal() System.out.printf("%.2f\n", r1.getTotal()); System.out.println((r1.getTotal()== 4.94)? "OK” : "FAIL"); } }
11
import java.util.Scanner; public class CashRegisterTest { public static void main(String[] args) { /* write code to create Scanner object input */ CashRegister r1 = new CashRegister(); // new instance /* write code: use input.nextDouble() to get an item’s value and store in a new variable price of type double */ r1.addItem(price); // CashRegister instance method /* repeat code to get second item price and add item to object */ System.out.println("Total cost is " + r1.getTotal()); System.out.println("The number of items bought is " + r1.getCount());
12
/* write additional code to: (i) create the remaining two instances of CashRegister (ii) add a number of item prices in each of them */ } } // end of class CashRegisterTest
13
14
15
16
17
18
19
20