Can you please tell me why is the first code giving me the right answer whereas the second one giving me a logically incorrect answer. Below are the two programs. The logic is the same. I want to know why the second program is not working even though the logic is the same. Is it due to the void functions. The program takes the input from the user such as the the purchase amount and the payment made, it then calculates amount to be returned in dollars, quarters, nickels etc... Please HELP and explain....

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

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());

}
}
Member Avatar for ztini

The issue you are having has nothing to do with whether your methods return void or not. The issue is integer division.

4 / 3 = 1
int / int = int (the .333333 is clipped)

4 / 3.0 = 1.33333
int / double = double

4.0 / 3 = 1.33333
double / int = double

4.0 / 3.0 = 1.33333
double / double = double

Thread close as there is duplicate here. Please follow discussion there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.