Solved

class and object

Posted on 2006-07-14
6
324 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
  • 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

832 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