Download Cs 101 Quiz No 4 Section 02

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 101 Section 02 Quiz No. 4
Problem a)
Define the necessary public variable to keep track of nextId that starts from 1, and increases by 1 each
time a new Inventory item is instantiated (recall the Inventory class and its tester class from quiz no. 3).
Adjust the constructor not to take the id as a parameter, but set it according to the value of nextId. Discard
the setId method.
Make the necessary changes in the tester program to instantiate the inventory items.
At the end of the loop, add the necessary statement(s) to display the value of nextId.
Problem b)
Write a Java program to compute and display the payment amount from one line of input in the form,
itemPrice*itemAmount
as long as the input line is not equal to “end”. Use the StringTokenizer, Integer, and Double classes to
achieve this task.
When the input ends, display the number of items purchased (number of input lines other than “end”) and
total payment.
Sample execution:
12.45*4
You should pay 49.8
10.55*3
You should pay 31.650000000000002
2*5
You should pay 10.0
0.55*1
You should pay 0.55
end
Total payment for 4 items will be 92.0
Solution for problem a:
import java.text.NumberFormat;
public class InventoryQ4 {
public static int nextId = 1;
private String name;
private int id, amount;
private double price;
public InventoryQ4 (String name, int amount, double price){
this.name = name.toUpperCase() ;
this.id = nextId++;
this.setAmount (amount);
this.setPrice (price);
}
public String getName (){
return name;
}
public int getId(){
return id;
}
public int getAmount (){
return amount;
}
public double getPrice () {
return price;
}
public void setAmount (int amount) {
if (amount<1)
amount=0;
this.amount = amount ;
}
public void setPrice (double price) {
this.price =Math.abs (price) ;
}
public String toString () {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return "Inventory no: "+id+" name: "+name+
" price: "+fmt.format(price)+" amount:"+amount;
}
public double computeTotalPrice () {
return amount*price;
}
}
import java.util.Scanner;
import java.util.Random;
public class quiz4section2 {
private int randomInteger (int a, int b){
Random rnd = new Random ();
return (a + rnd.nextInt (b-a+1) );
}
public static void main (String [] args) {
quiz4section2 tpo = new quiz4section2 ();
int n = tpo.randomInteger (2,5) ;
Scanner keyboard = new Scanner (System.in) ;
InventoryQ4 item;
String itemName ;
double itemPrice, paymentAmount ;
int itemAmount, purchaseAmount ;
for (int i=1;i<=n;i++) {
System.out.print ("Name of inventory: ");
itemName = keyboard.next();
// discard reading the id
System.out.print ("Amount on hold: ");
itemAmount = keyboard.nextInt();
System.out.print ("Price of inventory: ");
itemPrice = keyboard.nextDouble();
item = new InventoryQ4 (itemName, itemAmount, itemPrice) ;
System.out.println (item) ;
System.out.print ("How many do you want to buy?");
purchaseAmount = keyboard.nextInt ();
itemAmount = item.getAmount();
if (purchaseAmount>itemAmount){
System.out.println ("You can only buy "+itemAmount+" "+
item.getName() + "(s)." ) ;
purchaseAmount = itemAmount;
}
if (purchaseAmount>0) {
paymentAmount = purchaseAmount * item.getPrice();
item.setAmount (itemAmount-purchaseAmount);
System.out.println ("You should pay "+paymentAmount);
System.out.println("New amount for this item: “+item.getAmount());
}
}
System.out.println ("NextId="+InventoryQ4.nextId);
}
}
Solution for problem b:
import java.util.Scanner;
import java.util.StringTokenizer;
public class quiz4section2problemb {
public static void main (String [] args) {
Scanner keyboard = new Scanner (System.in) ;
StringTokenizer tokens ;
String line ;
double itemPrice, paymentAmount, total = 0 ;
int itemAmount, n=0 ;
line = keyboard.nextLine();
while (line.equalsIgnoreCase("end") == false ) {
tokens = new StringTokenizer (line,"*");
itemPrice = Double.parseDouble(tokens.nextToken());
itemAmount = Integer.parseInt(tokens.nextToken());
paymentAmount = itemPrice * itemAmount ;
n++;
total += paymentAmount ;
System.out.println ("You should pay "+paymentAmount);
line = keyboard.nextLine();
}
System.out.println ("Total payment for " + n + " items will be " + total ) ;
}
}