I can't get the object array in lab9 class (named "thelist") to be populated with objects from the Foreign class. please see line 35 in lab9 class. I've tried using toString, useing a Foreign type variable as a parameter in a addData(), a addData() method ect... I am really really stuck. I am just learning Java and there are no Tutors available at my school. Thank you in advance for any help, ideas, or solution in advance. These two classes compile but they dont work.
I've spent two weeks trying to analyze and learn how to do the step on line 35 and I have hit a brick wall, I just can not figure out why the things I have tried didn't work. I really want to learn how to do this.
Theses are the instructions for the homework assignment:
Modify your prior Lab 8 by adding an array of objects. Within the loop, instantiate each individual object. Make sure the user cannot keep adding another Foreign conversion beyond your array size.
After the user selects quit from the menu, prompt if the user want to display a summary report. If they select ‘Y’ then, using your array of objects, display the following report:
Item Conversion Dollars Amount
1 Japanese Yen 100.00 32,000.00
2 Mexican Peso 400.00 56,000.00
3 Canadian Dollar 100.00 156.00
etc.
Number of Conversions = 3
=======================================================
here are the two classes I wrote for this assignment
=======================================================
import java.util.Scanner;
public class lab9
{
public static void main(String[] args)
{
final int Max = 10;
int choice;
String s;
char c; //choice for summary report
int i = 0;
Foreign foreignObject = new Foreign();
Foreign[] thelist = new Foreign[Max];//array of 10 reference variables of type "Foreign"
Scanner Keyboard =new Scanner(System.in);
Foreign.title();
do
{
Foreign.menu();
choice=Foreign.number;//gets choice number entered for choice
if(i < thelist.length)
{
thelist[i] = new Foreign();//gives actual reference to memory large enough for a object of Foreign class
thelist[i]. getDollars();//can't get method or to toString to populate thelist object array
i++;
foreignObject.vertical();
}
}while (choice != 0 && i <= Max );
System.out.print("\t\t\nWould you like to display a summary report? (y/n): ");
s = Keyboard.nextLine();
c = s.charAt(0);
switch (c)
{
case 'y':
case 'Y':
System.out.println("\nItem\t\tConversion\t\tDollars\t\tAmount");
for ( i = 0 ; i < thelist.length ; i++)
{
System.out.println(i+1 + "\t");
System.out.print(thelist[i] + "\t");
}
}
foreignObject.counter();
}
}
//======================================================================================//================================================
import java.util.Scanner;
import java.text.*;
public class Foreign
{
public static int number;//number entered for choice
public static int n;
private static int count = 0;//count of the number conversions made
private double dollars, amount;
public static double rate;
public static String country ;
public static String[] cName= {"","Canadian Dollars","Mexican Peso","Japanese Yen","Euros"};
public static double[] rates= {0,1.03,10.55,117.57,.8407};//0 and "" so index "i" matches number 1 choice
DecimalFormat rF = new DecimalFormat("##00.##");
DecimalFormat money = new DecimalFormat("###,###,#00.##");
static Scanner read=new Scanner(System.in);
public Foreign()// constructor to initialize numerics to 0 and country to "" null
{
country = " ";
number=0;
rate = 0.0;
amount = 0.0 ;
dollars = 0.0;
}
public static void title()// a static method to display title
{
System.out.println("\t\tForeign Exchange\n\n");
}
public static void menu()
{
System.out.println("\t\t1. U.S. to Canadian Dollars");
System.out.println("\t\t2. U.S. to Mexican Pesos");
System.out.println("\t\t3. U.S. to Japanese Yen");
System.out.println("\t\t4. U.S. to European Euros");
System.out.println("\t\t0. Quit\n\n");
System.out.println("\t\tEnter your choice: ");
do
{
n = read.nextInt();
if (n > 4 || n < 0)
System.out.println("\t\tPlease enter 1 through 4 or 0 to quit:\n");
}while (n > 4 || n < 0);
number = n ;
country = cName [number];
rate=rates[number];
}
public void getDollars()
{
System.out.print("\t\tPlease enter amount in U.S. dollars:\n");
Scanner input = new Scanner(System.in);
dollars = input.nextDouble();
count++;
amount=dollars*rate;
}
public void vertical()
{
System.out.println("\t\tCountry = " + country );
System.out.println("\t\tRate = " + rF.format(rate) );
System.out.println("\t\tDollars = " + money.format(dollars) );
System.out.println("\t\tValue = " + money.format(amount) );
}
public String toString() //a toString()method to display the data horizontally via a string returned to lab9
{
String line;
line = "\n\n" + country + "\t\t " + money.format(dollars) + "\t\t " + money.format(amount)+"\n\n";
return line;
}
public void counter()
{
System.out.println("\nThe total number conversions made is " + count);
}
}