Car.java import java.util.Calendar; public class Car { private String automobile
ID: 3685040 • Letter: C
Question
Car.java
import java.util.Calendar;
public class Car {
private String automobile;
private String model;
private int year;
Car(String autmobie, String model, int year){
this.automobile = autmobie;
this.model = model;
this.year = year;
}
public String toString(){
String s;
s = year +" "+automobile+" "+model;
return s;
}
public boolean antique(){
boolean flag;
//determine current year
Calendar today = Calendar.getInstance();
int currentYear = today.get(Calendar.YEAR);
if( (currentYear - year) > 25)
flag = true;
else
flag = false;
return flag;
}
}
Car_Test.java
public class Car_Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter Automobile: ");
String autommobile = in.next();
System.out.println("Please Enter Model: ");
String model = in.next();
System.out.println("Please Enter Year: ");
int year = in.nextInt();
Car c = new Car(autommobile, model, year);
System.out.println(c.toString());
boolean status = c.antique();
if(status == true)
System.out.println("The car is an antique");
else
System.out.println("The car is not an antique");
}
}
Output:
Please Enter Automobile:
Toyoto
Please Enter Model:
Camry
Please Enter Year:
1990 Toyoto Camry
The Car is an antique
0 cse.usf.edu James Bay - Let It Go - YouTube Project 12: Cars from File www.cse.usf.edu/-turnerr/Programming Concepts/130_Project 12 C - Add a constructor to the Car class from Project 11 that takes a file Scanner object as its only parameter. The file will consist of attributes of cars As read from the keyboard in Project 11 - Let the new constructor read three lines from the file, using the scanner object passed by the caller, to get the attributes of a single car, and use those values to initialize a Car object: . Make . Model . Year - Remember that you will need to clear a blank line from the Scanner after reading the year as an integer.Explanation / Answer
import java.util.Calendar;
import java.util.Scanner;
public class Car {
private String automobile;
private String model;
private int year;
public Car(Scanner scanner) {
// TODO Auto-generated constructor stub
try {
this.automobile = scanner.next();
this.model = scanner.next();
this.year = scanner.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
}
Car(String autmobie, String model, int year) {
this.automobile = autmobie;
this.model = model;
this.year = year;
}
public String toString() {
String s;
s = year + " " + automobile + " " + model;
return s;
}
public boolean antique() {
boolean flag;
// determine current year
Calendar today = Calendar.getInstance();
int currentYear = today.get(Calendar.YEAR);
if ((currentYear - year) > 25)
flag = true;
else
flag = false;
return flag;
}
}
import java.io.File;
import java.util.Scanner;
public class CarsFromFile {
public static void main(String[] args) {
Scanner scanner = null, fileScanner = null;
try {
scanner = new Scanner(System.in);
String fileName = "";
File file = null;
do {
System.out.print("Please Ente File Name:");
fileName = scanner.next();
file = new File(fileName);
if (file.exists()) {
break;
} else {
System.out.println("File " + fileName
+ " was not found. Please try again");
}
} while (true);
Car cars[] = new Car[100];
int counter = 0;
fileScanner = new Scanner(file);
while (fileScanner.hasNext()) {
cars[counter] = new Car(fileScanner);
counter++;
}
System.out.println("End of file reached");
System.out.println((counter) + " cars Created.");
for (int i = 0; i < counter; i++) {
System.out.println(cars[i].toString());
}
System.out.println("Program completed");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
scanner.close();
fileScanner.close();
}
}
}
cars.txt
Toyata
Prius
2012
Ford
Falcon
1963
Fiat
500
2014
OUTPUT:
Please Ente File Name:cat.txt
File cat.txt was not found.
Please try again
Please Ente File Name:cars.txt
End of file reached
3 cars Created.
2012 Toyata Prius
1963 Ford Falcon
2014 Fiat 500
Program completed