anonymou
2013-05-12 14:50:04 UTC
I have it that I can hire a car but no matter what I try I can't get a menu or a vector to work could someone please help
Code:
import java.util.Scanner;
import java.util.Vector;
class carTest{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
String make;
int year;
String reg;
Double cost;
int days;
Vector data = new Vector ();
Car[] c = new Car[1];
for(int j = 0; j < c.length; j++)
{
//get input for the 3 car objects stored in the array
System.out.println("Enter make");
make = in.nextLine();
System.out.println("Enter year");
year = in.nextInt();
System.out.println("Enter reg");
reg = in.nextLine();
System.out.println("Enter days");
days = in.nextInt();
//create three new car objects. referenced by each element of the aray
c[j] = new Car(make, reg, year, days);
//compute the carhire cost
c[j].computeCost(days);
}
for(int i = 0; i < c.length; i ++)
{
c[i].display();
System.out.println();
}
}
}//end class
Code2:
//Car Hire example program
class Car
{
// attributes
public String make;
public int year;
public String reg;
public int numDays;
private double cost;
// methods
public Car()
{
cost = 0.0;
}
// the constructor
public Car(String m, String r, int y, int days)
{
make = m;
year = y;
reg = r;
numDays = days;
cost = 0.0;
}
//methods to read the attributes
public String getMake()
{
return make;
}
public int getyear()
{
return year;
}
public String getReg()
{
return reg;
}
public int getNumDays()
{
return numDays;
}
public double getCost()
{
return cost;
}
//method to compute the total car hire cost
public double computeCost(int days)
{
if (days > 10){
System.out.println("Error");
}
else if (year >= 5){
cost = (days * 10);
}
else if (days >= 6){
cost = (days * 15) - ((days * 15)*0.04);
}
else {
cost = days * 15;
}
return cost;
}
public void display(){
System.out.println("\n\nMake: " + make);
System.out.println("\nyear: " + year);
System.out.println("\nreg: " + reg);
System.out.println("\nNum Days: " + numDays);
System.out.println("\nCost: " + cost);
}
}//end class