The first program compiles fine and is also logically corrrect but the second program also compiles but is logically incorrect!! Why???? Is there something wrong with the methods in the class????
import java.util.*;
class CashRegister{
private int dollar=0;
private int quarter=0;
private int dime=0;
private int nickel=0;
private double pennies=0.0;
private double puramnt=0.0;
private double change1=0.0;
public CashRegister()
{dollar=0;
quarter=0;
dime=0;
nickel=0;
pennies=0;
}
Scanner in =new Scanner(System.in);
public void recordPurchase()
{System.out.println("Enter purchase amount:");
this.puramnt=in.nextDouble();
}
public void enterDollars()
{System.out.println("Enter dollars:");
this.dollar=in.nextInt();
}
public void enterQuarters()
{System.out.println("Enter quarters:");
this.quarter=in.nextInt();
}
public void enterDimes()
{System.out.println("Enter dimes:");
this.dime=in.nextInt();
}
public void enterNickels()
{System.out.println("Enter nickels:");
this.nickel=in.nextInt();
}
public void enterPennies()
{System.out.println("Enter pennies:");
this.pennies=in.nextDouble();
}
public double giveChange()
{double change=(dollar+quarter*0.25+dime*0.1+nickel*0.05+pennies*0.01)-puramnt;
change1=change;
return change;
}
public int giveDollars(){
dollar=(int)change1;
return dollar;
}
public int giveQuarters(){
quarter=(int)((change1-dollar)/0.25);
return quarter;}
public int giveDimes(){
dime=(int)((((change1-dollar)/0.25)-quarter)/0.4);
return dime;}
public int giveNickels(){
nickel=(int)(((((change1-dollar)/0.25)-quarter)/0.4-dime)/0.5);
return nickel;
}
public double givePennies(){
pennies=((((((change1-dollar)/0.25)-quarter)/0.4-dime)/0.5-nickel)*5);
return pennies;
}
}
public class Ch4q1{
public static void main(String[] args)
{CashRegister register = new CashRegister();
register.recordPurchase();
register.enterDollars();
register.enterQuarters();
register.enterDimes();
register.enterNickels();
register.enterPennies();
System.out.println("Change="+register.giveChange());
System.out.println("Dollars:"+register.giveDollars());
System.out.println("Quarters:"+register.giveQuarters());
System.out.println("Dimes:"+register.giveDimes());
System.out.println("Nickels:"+register.giveNickels());
System.out.println("Pennies:"+register.givePennies());
}
}
The Second Program(Compiles but is logically wrong):-
import java.util.*;
class CashRegister{
private int dollar=0;
private int quarter=0;
private int dime=0;
private int nickel=0;
private double pennies=0.0;
private double puramnt=0.0;
public CashRegister()
{dollar=0;
quarter=0;
dime=0;
nickel=0;
pennies=0;
}
Scanner in =new Scanner(System.in);
public void recordPurchase()
{System.out.println("Enter purchase amount:");
puramnt=in.nextDouble();
}
public void enterDollars()
{System.out.println("Enter dollars:");
dollar=in.nextInt();
}
public void enterQuarters()
{System.out.println("Enter quarters:");
quarter=in.nextInt();
}
public void enterDimes()
{System.out.println("Enter dimes:");
dime=in.nextInt();
}
public void enterNickels()
{System.out.println("Enter nickels:");
nickel=in.nextInt();
}
public void enterPennies()
{System.out.println("Enter pennies:");
pennies=in.nextDouble();
}
public double change=(dollar+quarter*0.25+dime*0.1+nickel*0.05+pennies*0.01)-puramnt;
public int giveDollars(){
dollar=(int)change;
return dollar;
}
public int giveQuarters(){
quarter=(int)((change-dollar)/0.25);
return quarter;}
public int giveDimes(){
dime=(int)((((change-dollar)/0.25)-quarter)/0.4);
return dime;}
public int giveNickels(){
nickel=(int)(((((change-dollar)/0.25)-quarter)/0.4-dime)/0.5);
return nickel;
}
public double givePennies(){
pennies=((((((change-dollar)/0.25)-quarter)/0.4-dime)/0.5-nickel)*5);
return pennies;
}
}
public class Ch4q2{
public static void main(String[] args)
{CashRegister register = new CashRegister();
register.recordPurchase();
register.enterDollars();
register.enterQuarters();
register.enterDimes();
register.enterNickels();
register.enterPennies();
System.out.println("Dollars:"+register.giveDollars());
System.out.println("Quarters:"+register.giveQuarters());
System.out.println("Dimes:"+register.giveDimes());
System.out.println("Nickels:"+register.giveNickels());
System.out.println("Pennies:"+register.givePennies());
}
}
Both the codes are same except for a few differences. Why does the second one give a runtime error???