Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# class and object

Posted on 2006-07-14
Medium Priority
331 Views
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

0
Question by:Bushmouse
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2

LVL 35

Expert Comment

ID: 17108015
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

LVL 35

Expert Comment

ID: 17108043
> public class Converter

This should be

actually.
0

Author Comment

ID: 17108710
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

LVL 10

Expert Comment

ID: 17110109
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 Comment

ID: 17111252
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

LVL 10

Accepted Solution

mukundha_expert earned 2000 total points
ID: 17112226
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month12 days, 6 hours left to enroll