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???

What errors???? At compile time??????
Isn't that annoying?

commented: Yes, it is indeed :) +16

The logical error....the programming logic is the same but the results differ...i wanna know WHY??? Is there some problem with the methods????

@ztini it doesn't help.... are you sure... i tried by changing all fields to double yet the answer is not cuming...!!! HELP

Member Avatar for ztini
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;
}

Same issue, you're casting doubles to int. This drops everything to the right of the decimal. Post your revised code, lets have a look.

double d = 4.99;

		int i = (int) d;        // i == 4		
		double d2 = (int) d;    // d2 == 4.0

Casting to an int always clips the double at the decimal.

Member Avatar for ztini

Its also considered good form to group all your global variables together. You also never want to declare your globals as public. Otherwise I can access them like this:

CashRegister.change = 0;

Which of course breaks the program.

If you need to provide access to your globals for some reason, use get/set's (and in a perfect world, return a copy).

I used the same logic in the first program i posted right at the start, over there also i have casted doubles as ints. The answer is right in the first one but not in the second program. In the second program if you run and see the output everything is 0(dollars, nickels etc...). This has led me to belive that there is some problem with the methods and i want to know what is the problem......

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.