Results 1 to 12 of 12
  1. #1
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default Need some help please

    I have an assignment ive been working on and cant figure out how to write the code. I have most of the core info already written but cant figure how to do the methods. all of it deals with classes and objects

    Create a class called “Foreign” with 5 instance data: choice (int), country (String), dollars (double), conversion value (double), and converted amount (double). Create 7 methods: 1 – a constructor to initialize data (numerics to zero and country to null “”); 2 – a method to display the menu and get a choice (from lab5); 3 - a method to return the choice selected; 4 - a method to get the dollars input from the keyboard, and then assign the appropriate country and conversion rate using the menu choice; 5 – a method to calculate the new converted amount; 6 – a method to display the values vertically with descriptions (see below); and 7 – a toString() method to display the data horizontally with one space between each value (see below). Your toString() method must conform to the predefined syntax for all toString() methods. You do not need the count, total, and average from lab5. Write a second file “Lab6.java” that uses this class, repeats until Quit is selected, instantiates an object and calls all the methods.

    Method 6 to display values vertically looks like this:

    Country = Japanese Yen
    Rate = 123.78
    Dollars = 100.00
    Value = 12,378.00

    Method 7 to display the data horizontally looks like this:

    Japanese Yen 123.78 100.00 12,378.00

    Email me your finished source code lab assignment as an attachment. Just attach the two files that you save as .java (Foreign.java and Lab6.java).

    Now heres my code to support most of the tasks



    import java.util.Scanner;
    import java.text.NumberFormat;

    public class Foreign
    {
    {
    Scanner keyboard = new Scanner (System.in);
    int choice;
    double result, dollars, conversion_value, converted_amount;
    String country = "U.S. Dollars";
    NumberFormat dollarForm = NumberFormat.getCurrencyInstance();

    public Foreign( )
    {
    conversion_value = 0.0; //thought this was the format to creat a constructor to initialize data
    } //this would take care of the first method he asks for in the lab.

    do
    {
    System.out.println("============================== ========================"); // this is the menu that takes care of method 2 in lab
    System.out.println(" Foreign Exchange Menu");
    System.out.println("============================== ========================\n\n");
    System.out.println("1. U.S. to Canada");
    System.out.println("2. U.S. to Mexico");
    System.out.println("3. U.S. to Japan");
    System.out.println("4. U.S. to Euro");
    System.out.println("0. Quit\n");
    System.out.print("\nPlease enter your choice:");
    choice = keyboard.nextInt();

    System.out.println(choice); // takes care of method 3 to return choice selected by user

    if (choice >= 1 && choice <= 4) //the conversion rates depending on which choice was selected this is part of method 4
    {
    System.out.println("\n============================ ==========================");// second part of method 4 to have user input amount to be converted
    System.out.println(" Foreign Exchange Service ");
    System.out.println("============================== ========================\n\n");
    System.out.print("\nPlease enter the amount of U.S. Dollars: ");
    dollars = keyboard.nextDouble();

    switch (choice)
    {
    case 1:
    conversion_value = 1.1553;
    country = "Canadian Dollars";
    break;

    case 2:
    conversion_value = 10.550;
    country = "Mexican Pesos";
    break;

    case 3:
    conversion_value = 117.57;
    country = "Japanese Yen";
    break;

    case 4:
    conversion_value = 0.8407;
    country = "European Euros";
    break;
    }

    converted_amount = dollars * conversion_value; //method 5 to do the conversion
    System.out.println(dollarForm.format(dollars) + " * " + conversion_value + " is " + converted_amount);
    System.out.println("You now have " + converted_amount + " " + country + "\n\n");

    }
    else if (choice > 4)
    {
    System.out.println("============================== ========================");
    System.out.println(" Please select 1 through 4 or 0 to quit: ");
    System.out.println("============================== ========================\n\n\n");
    }
    }
    while (choice != 0);


    }
    }




    Im totally lost some help would be great.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    No one wants to do your homework here, because this is a forum, a discussion board. So please ask your question more clearly, on what you have stuck with at the time? Sure, I'll help you to solve this, but not in this way.

  3. #3
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    ever heard of "["CODE"]" and "["/CODE"]"

    Also, maybe you should draw the structure of what you want that the program will do. This helps a lot, like in discovering where it goes wrong etc.
    Last edited by blackstormattack; 03-08-2009 at 12:45 PM.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    JDAWG did specify what he was having trouble with:
    ... but cant figure how to do the methods.
    Here's a tutorial on methods:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    blackstormattack you should learn to be more respectful. I am sure you also learned the code somewhere and where not born with it.

    Also, even if he doesn't put the tags, it make a difference since his code perfectly fits the window.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Still, code tags will make it easier for others to read the code, and this will likely increase the chances that someone will take the time to read the code and offer help.

    Again, place the tag [cod&#101;] above your block of code and the tag [/cod&#101;] below the block like so:

    Java Code:
    [cod&#101;]
      // your code goes here...
    [/cod&#101;]

  7. #7
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    I am just starting to learn java so please bare with me.

    Ill start with the first part:

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    {
    Scanner keyboard = new Scanner (System.in);
    int choice;
    double result, dollars, conversion_value, converted_amount;
    String country = "U.S. Dollars";
    NumberFormat dollarForm = NumberFormat.getCurrencyInstance();
    
    public Foreign( )
    {
    conversion_value = 0.0; //thought this was the format to creat a constructor to initialize data 
    } //this would take care of the first method he asks for in the lab.
    shouldnt this set of code says what the variables are and then a constructor to initialize the data? Is this format correct? How I understand this is I take the class name and restate it then in the brackets I state what values I am setting the code to start at which initializes it also? Am I correct in saying this?
    Last edited by JDAWG; 03-08-2009 at 07:56 PM.

  8. #8
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    Well I think I answered some of my question regarding the code and /code. It sets up the diplay the code for easier reading???

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JDAWG View Post
    Well I think I answered some of my question regarding the code and /code. It sets up the diplay the code for easier reading???
    At the same time you have to intend the code more clearly. I think you are working on Notepad and not care of code intending. Anyway, this is better than earlier.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JDAWG View Post
    I am just starting to learn java so please bare with me.

    Ill start with the first part:

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    {
    Scanner keyboard = new Scanner (System.in);
    int choice;
    double result, dollars, conversion_value, converted_amount;
    String country = "U.S. Dollars";
    NumberFormat dollarForm = NumberFormat.getCurrencyInstance();
    
    public Foreign( )
    {
    conversion_value = 0.0; //thought this was the format to creat a constructor to initialize data 
    } //this would take care of the first method he asks for in the lab.
    shouldnt this set of code says what the variables are and then a constructor to initialize the data? Is this format correct? How I understand this is I take the class name and restate it then in the brackets I state what values I am setting the code to start at which initializes it also? Am I correct in saying this?
    According to your assignment, in a method you have to initialize all data. But you don't have initialize all of them. Can you do it and show here? Then we can look at what you have done and comment on it for the next step.

  11. #11
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Lab6
    {
    	public static void main(String[] args)
    	{
    		String more = "Y";
    	   Foreign exchange = new Foreign();
    		Scanner Keyboard = new Scanner(System.in);	
    		
    	do
    		{	
    		exchange.menu();	
    		exchange.choice();
    		exchange.dollars();
    		exchange.vertical();
    		System.out.println("\n" + exchange);
    				
    		System.out.print("\nWould you like to enter another exchange? Y/N ");
    		more = Keyboard.nextLine();
    		}
    	while (Character.toUpperCase(more.charAt(0)) == 'Y'); 
    	
    	
    	}
    }
    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
     public class Foreign
    {
    	private int choice;
    	private String country;
    	private double dollars, rate, amount;
    	Scanner Keyboard = new Scanner(System.in);
       NumberFormat dollarFormat = NumberFormat.getCurrencyInstance();
    
    	public Foreign()
    	{
    				dollars = 0.0;
    				rate = 0.0;
    				amount = 0.0;
    				country = "null";
    	}
     		
    	public void menu()
    	{
    		System.out.println("======================================================");
    		System.out.println("                  Foreign Exchange Menu");
    		System.out.println("======================================================\n\n");
    		System.out.println("1. U.S. to Canada");
    		System.out.println("2. U.S. to Mexico");
    		System.out.println("3. U.S. to Japan");
    		System.out.println("4. U.S. to Euro");
    		System.out.print("\nPlease enter your choice:");
    
     	}
    	
    	public void choice()
    	{
    	choice = Keyboard.nextInt();
    	}
    		
    	public int getchoice()
    	{
    		return choice;
    	}
    
    	public void dollars()
    	{
    		System.out.print("\nEnter the amount of U.S. Dollars you want to convert: ");
    	 	dollars = Keyboard.nextDouble();
    	 
    		if (choice >= 1 && choice <= 4)
    	 
    		switch (choice)
    		{
    	 		case 1: 
    				rate = 1.2418;
    				country = "Canadian Dollars";
    			break;
    		
    	 		case 2:
    	 			rate = 14.5436;
    				country = "Mexican Pesos";
    			break;
    		
    	 		case 3:
    	 			rate = 88.9183;
    				country = "Japanese Yen";
    			break;
    		
    			case 4:
    	 			rate =  0.774078;
    				country = "Euros";
    			break;
    			}
    
    			amount = dollars * rate;
    	 }		
    	 	
    	public void vertical()
    	{
    	System.out.println("\nCountry = " + country);
    	System.out.println("Rate = " + rate);
    	System.out.println("Dollars = " + dollarFormat.format(dollars));
    	System.out.println("Value = " + amount);
    	}
    	
    	public String toString()
    	{
    		String line;
    		line = country + " " + rate + " " + dollars + " " + amount;
    		return line;
    	}
    }
    so here is what I finished with i still have a couple issue with how to make the code end or display a menu again if the choices arent either a number 1 through 4... Second i want to make i wrote the item right when my instructor asked to return the choice answer.... I start there for now thanks :D

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •