# class and object

I'm learning java and below is one of my exercise questions I'm on.  I'm sort of stumped as to how to approach this.  Sometimes it helps to see it to learn.  Anyhow, here's the question.

Intended to teach the following:

how to implement a simple class
how to use an object to keep track of data in a class
This project models a high school graduate who has relatives in the United States, Japan, and Europe. The graduate is getting money for graduation from relatives in each of the countries and wants to keep track in U.S. dollars of how much he has received. The program will have an interface with the following options:

Enter the number of U.S. dollars
The user will enter the number of U.S. dollars received and add it to the total dollars received so far. The program will then prompt the user for more input.

Enter the number of euros
The user will enter the number of euros received, convert it to U.S. dollars by multiplying by 1.24, and add it to the total dollars received so far. The program will then prompt the user for more input.

Enter the number of yen
The user will enter the number of yen received, convert it to U.S. dollars by multiplying by 0.0092 (or dividing by 108.7), and add it to the total dollars received so far. The program will then prompt the user for more input.

Exit the program
Print out the total collected in U.S. dollars and exit the program.

The user will enter the type of currency to use and the quantity, which will be added to the total. The user then will be prompted for the next entry. When option 4 is selected, the program will print out the total savings and exit.

For this project, you will implement a Gifts object that will keep track of the total amount given. Pseudocode for this object is given below:

INSTANCE VARIABLES
totalCollected

INSTANCE METHODS
enterUSdollars
enterEuros
enterYen
totalSavings

###### Who is Participating?

x

Commented:
Declare totalsavings,EUROS_TO_DOLLARS,YEN_TO_DOLLARS

>>    public void totalSavings() {
>>         totalSAvings = totalCollected;
>>          } //end method

what is this method doing??

this method returns void, but you are returning some value . look into it

add semicolons in few statements in which they are misssing

otherwise its ok
0

Commented:
Hi Bushmouse

the first thing you have to do is to create your class. Then inside your class define the INSTANCE VARIABLES and INSTANCE METHODS. In these methods you will need to pass parameters in order to do the calculations needed, like converting from one currency to the other. A very general design would be

public class Converter
{
public int totalCollected = 0;

public void enterUSdollars(int dollars)
{
totalColelcted = totalCollected + dollars;
}
}

YOu will do likewise for the rest of the methods. In some that you will need multiplication simply multiply the amount you receive by the appropriate factor (1.24 etc) before you add it. If you need any more help let us know.
Cheers
0

Commented:
> public class Converter

This should be

actually.
0

Author Commented:
This is what I had come up with so far.  I was unsure as to how to implement the class, object, etc..  Maybe I'm going about it all wrong.  Where would the class you created in your above comment fit into this?  Or where am I going wrong with the approach to this?

import java.util.*;

//main(): application entry point
public static void main(String[] args) {
double dollar = 0.0;
double yen = 0.0;
int option = 0;

//define constant
final double EUROS_TO_DOLLARS = 1.24;
final double YEN_TO_DOLLARS = 0.0092;

//setup input stream
Scanner stdin = new Scanner(System.in);

// display conversion options
System.out.print("Select one of the following options ");
System.out.println("by entering 1 through 4");
System.out.println(" ");
System.out.println("Enter number of U.S. dollars = 1");
System.out.println("Enter number of euros = 2");
System.out.println("Enter number of yen = 3");
System.out.println("Show total and Exit the program = 4");
System.out.println(" ");

// get conversion option

System.out.print("Enter option 1 through 4: ");
option = stdin.nextInt();

while ((option >= 1) || (option <= 4)) {

// convert option 1
if (option == 1) {
System.out.print("Enter dollars: ");
dollar = stdin.nextDouble();
double dollar = dollar;
}

// convert option 2, euros to dollars
else if (option == 2) {
System.out.print("Enter euros: ");
euro = stdin.nextDouble();
double dollar = EUROS_TO_DOLLARS * euro;
}

// convert option 3, yen to dollars
else if (option == 3) {
System.out.print("Enter yen: ");
yen = stdin.nextDouble();
double dollar = YEN_TO_DOLLARS * yen;
}

else if (option == 4) {
System.exit(1);}

else if ((option < 1) || (option > 4)) {
System.out.println(option + " is an invalid option");
}

Sum totalCollected =+ dollar
System.out.println("");
System.out.println("");
System.out.print("Select one of the following options ");
System.out.println("by entering 1 through 4");
System.out.println(" ");
System.out.println("Enter number of U.S. dollars = 1");
System.out.println("Enter number of euros = 2");
System.out.println("Enter number of yen = 3");
System.out.println("Show total and Exit the program = 4");
System.out.println(" ");

// get conversion option

System.out.print("Enter option 1 through 4: ");
option = stdin.nextInt();}
}
}
0

Commented:
Hi Bushmouse,

what you have done so far is right. only few errors in declaration.

Declare ,
>>   //main(): application entry point
>>     public static void main(String[] args) {
>>     double dollar = 0.0;
>>     double yen = 0.0;
>>     int option = 0;
after this,
double euro = 0;
double totalCollected = 0 ;

then ,
else if (option == 4) {
System.out.println( "Total -->" + totalCollected ) ;
System.exit(1);}

>>Sum totalCollected =+ dollar
totalCollected = totalCollected +   dollar;

this class will do what ever you have specified ( getting input from user and printing the dollar value ) .

what ever you have written is also a class. but it don have any methods on its own. thats all. don confuse
0

Author Commented:
I've made some changes to include adding methods.  Could you take one more look at this?

// Purpose: Energy Conversion

import java.util.*;

int totalCollected;

totalCollected = 0;
} //end method

public void enterUSdollars(int dollar) {
USdollars = dollar;
totalCollected = totalCollected + USdollars;
} //end method

public void enterEuros(int euro) {
USdollars = EUROS_TO_DOLLARS * euro;
totalCollected = totalCollected + USdollars;
} //end method

public void enterYen(int yen) {
USdollars = YEN_TO_DOLLARS * yen;
totalCollected = totalCollected + USdollars;
} //end method

public void totalSavings() {
totalSAvings = totalCollected;
} //end method

//main(): application entry point
public static void main(String[] args) {
int dollar = 0;
int yen = 0;
int option = 0;

//define constant
final double EUROS_TO_DOLLARS = 1.24;
final double YEN_TO_DOLLARS = 0.0092;

//setup input stream
Scanner stdin = new Scanner(System.in);

// display conversion options
System.out.print("Select one of the following options ");
System.out.println("by entering 1 through 4");
System.out.println(" ");
System.out.println("Enter number of U.S. dollars = 1");
System.out.println("Enter number of euros = 2");
System.out.println("Enter number of yen = 3");
System.out.println("Show total and Exit the program = 4");
System.out.println(" ");

// get conversion option

System.out.print("Enter option 1 through 4: ");
option = stdin.nextInt();

while ((option >= 1) || (option <= 4)) {

// convert option 1
if (option == 1) {
System.out.print("Enter dollars: ");
dollar = stdin.nextDouble();
dollar.enterUSdollars(dollar);
}

// convert option 2, euros to dollars
else if (option == 2) {
System.out.print("Enter euros: ");
euro = stdin.nextDouble();
euro.enterEuros(euro);
}

// convert option 3, yen to dollars
else if (option == 3) {
System.out.print("Enter yen: ");
yen = stdin.nextDouble();
yen.enterYen(yen);
}

else if (option == 4) {
savings = totalSavings
System.out.println("Total savings are" + savings);
System.exit(1);}

else if ((option < 1) || (option > 4)) {
System.out.println(option + " is an invalid option");
}

Sum totalCollected =+ dollar
System.out.println("");
System.out.println("");
System.out.print("Select one of the following options ");
System.out.println("by entering 1 through 4");
System.out.println(" ");
System.out.println("Enter number of U.S. dollars = 1");
System.out.println("Enter number of euros = 2");
System.out.println("Enter number of yen = 3");
System.out.println("Show total and Exit the program = 4");
System.out.println(" ");

// get conversion option

System.out.print("Enter option 1 through 4: ");
option = stdin.nextInt();}
}
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.