Results 1 to 7 of 7
- 03-18-2011, 11:17 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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-18-2011 at 11:59 PM.
- 03-18-2011, 11:22 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Copy the exact errors for us. Also wrap your code in code tags
[code ]
your code here
[/code]
- 03-18-2011, 11:26 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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; } }My errors are numerous: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); } }
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 errorsLast edited by Gaebril; 03-18-2011 at 11:28 PM.
- 03-18-2011, 11:36 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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-18-2011 at 11:42 PM.
- 03-18-2011, 11:54 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
- 03-19-2011, 12:01 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-19-2011, 12:05 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Classes and Objects Help
By collin389 in forum New To JavaReplies: 1Last Post: 12-14-2009, 12:44 AM -
classes as objects
By kroiz in forum New To JavaReplies: 4Last Post: 07-25-2009, 05:22 AM -
Compiling with Different Classes
By Moncleared in forum Advanced JavaReplies: 5Last Post: 02-03-2009, 06:20 AM -
Compiling and using jar file for custom classes
By MAILMIRZA in forum New To JavaReplies: 3Last Post: 01-12-2009, 04:56 PM -
Objects and Classes
By Aleve in forum New To JavaReplies: 8Last Post: 12-31-2007, 08:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks