Results 1 to 7 of 7
  1. #1
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Troubling compiling Classes and Objects assignment!

    I am new to the forums and to Java. I am having trouble on an assignment.
    Prompt:
    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.

    Now, I have the Foreign.java done and the Lab6.java. However, I am having issues getting it to compile, and most of the errors appear from my Foreign.java. Any ideas would be great. I essentially just get a bunch of "Foreign.java:40: class, interface, or enum expected"

    Java Code:
    :
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    Scanner read=new Scanner(System.in);
    private int choice;
    private String country;
    private double dollars, value, amount;
    NumberFormat dollarForm=NumberFormat.getCurrencyInstance();
    
    public Foreign()
    {
    choice=0.0;
    country="";
    dollars=0.0;
    value=0.0;
    amount=0.0;
    }
    
    	public void menu()
    	{
    	System.out.println("U.S. Dollar to Foreign Currency Exchange");
    	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=read.nextInt();
    	}
    
    public int getchoice()
    }
    return choice;
    }
    
    
    		public void dollars()
    		{
    		System.out.print("\nPlease enter the amount of U.S. Dollars: ");
    		dollars= read.nextDouble();
    					
    			switch (choice)
    			{
    			case 1:
    				value = 1.1553;
    				country = "Canadian Dollars";
    			break;
    					
    			case 2:
    				value = 10.550;
    				country = "Mexican Peso";
    			break;
    					
    			case 3:
    				value = 117.57;
    				country = "Japanese Yen";
    			break;
    					
    			case 4:
    				value = .8407;
    				country = "Euro";
    			break;
    			}	
    		}
    			
    public void amount()
    {
    amount=rate*dollars
    }
    	
    public void vertical()
    {
    System.out.println("Country = " +country);
    System.out.println("Rate = " +dollarForm.format(rate));
    System.out.println("Dollars = " +dollarForm.format(dollars));
    System.out.println("Converted value = " +dollarForm.format(value));
    }
    
    public String toString()
    {
    		String horizontal;
    		horizontal = country + " " + value + " " + dollars + " " + amount;
    		return horizontal;
    }
    }
    Last edited by Gaebril; 03-19-2011 at 12:59 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Copy the exact errors for us. Also wrap your code in code tags

    [code ]
    your code here
    [/code]

  3. #3
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Ah. Will do. Okay:

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    Scanner read=new Scanner(System.in);
    private int choice;
    private String country;
    private double dollars, value, amount;
    NumberFormat dollarForm=NumberFormat.getCurrencyInstance();
    
    public Foreign()
    {
    choice=0.0;
    country="";
    dollars=0.0;
    value=0.0;
    amount=0.0;
    }
    
    	public void menu()
    	{
    	System.out.println("U.S. Dollar to Foreign Currency Exchange");
    	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=read.nextInt();
    	}
    
    public int getchoice()
    }
    return choice;
    }
    
    
    		public void dollars()
    		{
    		System.out.print("\nPlease enter the amount of U.S. Dollars: ");
    		dollars= read.nextDouble();
    					
    			switch (choice)
    			{
    			case 1:
    				value = 1.1553;
    				country = "Canadian Dollars";
    			break;
    					
    			case 2:
    				value = 10.550;
    				country = "Mexican Peso";
    			break;
    					
    			case 3:
    				value = 117.57;
    				country = "Japanese Yen";
    			break;
    					
    			case 4:
    				value = .8407;
    				country = "Euro";
    			break;
    			}	
    		}
    			
    public void amount()
    {
    amount=value*dollars
    }
    	
    public void vertical()
    {
    System.out.println("Country = " +country);
    System.out.println("Rate = " +dollarForm.format(rate));
    System.out.println("Dollars = " +dollarForm.format(dollars));
    System.out.println("Converted value = " +dollarForm.format(value));
    }
    
    public String toString()
    {
    		String horizontal;
    		horizontal = country + " " + value + " " + dollars + " " + amount;
    		return horizontal;
    }
    }
    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class lab6
    {
    	public static void main(String[] args)
    	{
    		Foreign exchange=new Foreign();
    		Scanner read=new Scanner(System.in);
    		
    		do
    		{
    			exchange.menu();
    			exchange.getchoice();
    			
    			if (exchange.getchoice() >= 1 && exchange.getchoice() <= 4)
    			{
    			exchange.dollars();
    			exchange.amount();
    			exchange.vertical();
    			System.out.println("\n" +exchange);
    			}
    					else if (exchange.getchoice() > 4)
    					{
    					System.out.println("Please select 1 through 4, or 0 to quit");
    					}
    		}
    		while (exchange.getchoice() != 0);
    	}
    }
    My errors are numerous:

    Foreign.java:38: ';' expected
    public int getchoice()
    ^
    Foreign.java:40: class, interface, or enum expected
    return choice;
    ^
    Foreign.java:41: class, interface, or enum expected
    }
    ^
    Foreign.java:44: class, interface, or enum expected
    public void dollars()
    ^
    Foreign.java:47: class, interface, or enum expected
    dollars= read.nextDouble();
    ^
    Foreign.java:49: class, interface, or enum expected
    switch (choice)
    ^
    Foreign.java:53: class, interface, or enum expected
    country = "Canadian Dollars";
    ^
    Foreign.java:54: class, interface, or enum expected
    break;
    ^
    Foreign.java:56: class, interface, or enum expected
    case 2:
    ^
    Foreign.java:58: class, interface, or enum expected
    country = "Mexican Peso";
    ^
    Foreign.java:59: class, interface, or enum expected
    break;
    ^
    Foreign.java:61: class, interface, or enum expected
    case 3:
    ^
    Foreign.java:63: class, interface, or enum expected
    country = "Japanese Yen";
    ^
    Foreign.java:64: class, interface, or enum expected
    break;
    ^
    Foreign.java:66: class, interface, or enum expected
    case 4:
    ^
    Foreign.java:68: class, interface, or enum expected
    country = "Euro";
    ^
    Foreign.java:69: class, interface, or enum expected
    break;
    ^
    Foreign.java:70: class, interface, or enum expected
    }
    ^
    Foreign.java:73: class, interface, or enum expected
    public void amount()
    ^
    Foreign.java:78: class, interface, or enum expected
    public void vertical()
    ^
    Foreign.java:81: class, interface, or enum expected
    System.out.println("Rate = " +dollarForm.format(rate));
    ^
    Foreign.java:82: class, interface, or enum expected
    System.out.println("Dollars = " +dollarForm.format(dollars));
    ^
    Foreign.java:83: class, interface, or enum expected
    System.out.println("Converted value = " +dollarForm.format(value));
    ^
    Foreign.java:84: class, interface, or enum expected
    }
    ^
    Foreign.java:86: class, interface, or enum expected
    public String toString()
    ^
    Foreign.java:89: class, interface, or enum expected
    horizontal = country + " " + value + " " + dollars + " " + amount;
    ^
    Foreign.java:90: class, interface, or enum expected
    return horizontal;
    ^
    Foreign.java:91: class, interface, or enum expected
    }
    ^
    lab6.java:23: cannot find symbol
    symbol : method dollars()
    location: class Foreign
    exchange.dollars();
    ^
    lab6.java:24: cannot find symbol
    symbol : method amount()
    location: class Foreign
    exchange.amount();
    ^
    lab6.java:25: cannot find symbol
    symbol : method vertical()
    location: class Foreign
    exchange.vertical();
    ^
    Foreign.java:19: possible loss of precision
    found : double
    required: int
    choice=0.0;
    ^
    Foreign.java:38: missing method body, or declare abstract
    public int getchoice()
    ^
    33 errors
    Last edited by Gaebril; 03-19-2011 at 12:28 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Generally when I get the error stating class, enum, or interface expected I am missing a closing bracket or a semicolon out of place. The best step is to just re-evaluate your code and try and find what's missing. Right now I don't see anything wrong, however; I will keep looking, and maybe someone else will see what I can't.

    also, I believe switch cases should have a default case. This may have nothing to do with the problem(and probably doesn't) but try adding it, see what happens.

    notice anything odd about this?
    Java Code:
    public int getchoice()
    }
    return choice;
    }
    Last edited by sunde887; 03-19-2011 at 12:42 AM.

  5. #5
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Generally when I get the error stating class, enum, or interface expected I am missing a closing bracket or a semicolon out of place. The best step is to just re-evaluate your code and try and find what's missing. Right now I don't see anything wrong, however; I will keep looking, and maybe someone else will see what I can't.

    also, I believe switch cases should have a default case. This may have nothing to do with the problem(and probably doesn't) but try adding it, see what happens.

    notice anything odd about this?
    Java Code:
    public int getchoice()
    }
    return choice;
    }
    You sir....I love. I hate how something small like that can be passed by.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved it, and remember that the smallest mistake will make the program uncompilable, you need to keep a very detail oriented eye for these things. With time you will also learn what different compilation errors mean which will help you find the problem.

  7. #7
    Gaebril is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    I actually figured it was an error pertaining to { } but for some reason I was just wasn't finding the placement. It doesn't help when you scrutinize for hours, always best to take a short break and come back. Then the problem is obvious. Like this one.

Similar Threads

  1. Classes and Objects Help
    By collin389 in forum New To Java
    Replies: 1
    Last Post: 12-14-2009, 01:44 AM
  2. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 06:22 AM
  3. Compiling with Different Classes
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-03-2009, 07:20 AM
  4. Compiling and using jar file for custom classes
    By MAILMIRZA in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 05:56 PM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 09:05 AM

Posting Permissions

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