Solved

class and object

Posted on 2006-07-14
6
327 Views
Last Modified: 2013-11-23
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:

Gifts class definition

INSTANCE VARIABLES
totalCollected

INSTANCE METHODS
enterUSdollars
enterEuros
enterYen
totalSavings

0
Comment
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
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:girionis
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

by:girionis
ID: 17108043
> public class Converter

This should be

public class Gifts

actually.
0
 

Author Comment

by:Bushmouse
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?

// Purpose: Gift Conversion and total of gifts

import java.util.*;

public class Gifts {

      //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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:mukundha_expert
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 ,
add this to option 4,
 else if (option == 4) {
                System.out.println( "Total -->" + totalCollected ) ;
               System.exit(1);}
               
>>Sum totalCollected =+ dollar
instead,
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

by:Bushmouse
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.*;

public class Gifts {
      int totalCollected;
      
      public Gifts() {
            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;
            return totalSavings;
            } //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

by:
mukundha_expert earned 500 total points
ID: 17112226
Declare totalsavings,EUROS_TO_DOLLARS,YEN_TO_DOLLARS

>>    public void totalSavings() {
>>         totalSAvings = totalCollected;
>>          return totalSavings;
>>          } //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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 56
Better way to make a string with template variables in java 3 35
java example issue 5 47
Tagging and Merging on Branch 1 45
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question