Solved

class and object

Posted on 2006-07-14
6
321 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now