Unit 1 to 4
import java.util.Scanner;
public class Hack1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of cars: ");
double car = scanner.nextDouble();
int wheel = (int)(car * 4);
System.out.println("The number of wheels of the cars is " + wheel);
scanner.close();
}
}
Hack1.main(null);
Hack 2
Create a class named “Car” with the following attributes:
make (String) model (String) year (int) price (double) isUsed (boolean) Create a constructor that initializes all of these attributes.
Create a method named “printCarDetails()” that prints out all of the attributes of the car.
In the main method, create an array of three cars with the following details:
Car 1: Make = “Toyota”, Model = “Corolla”, Year = 2022, Price = 24999.99, isUsed = false Car 2: Make = “Honda”, Model = “Accord”, Year = 2018, Price = 18999.99, isUsed = true Car 3: Make = “Ford”, Model = “Mustang”, Year = 2020, Price = 34999.99, isUsed = true Loop through the array and print out the details of each car using the “printCarDetails()” method.
public class Car {
//instance variables
private String make;
private String model;
private int year;
private double price;
private boolean isUsed;
//constructors
public Car(String make, String model, int year, double price, boolean isUsed) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.isUsed = isUsed;
}
//getters
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
public boolean getIsUsed() {
return isUsed;
}
// method to print car details
public void printCarDetails() {
System.out.println(getMake() +" "+ getModel() +" " + getYear() +" "+ getPrice() +" "+ getIsUsed());
}
public static void main (String[] args) {
Car car1 = new Car("Toyota", "Corolla", 2022, 24999.99, false);
car1.printCarDetails();
}
}
Car.main(null);
Hack 3
Create a Car object named myCar with the following details: Make: “Toyota” Model: “Camry” Year: 2019 Price: 25000.0 Print the car details using the getMake(), getModel(), getYear(), and getPrice() methods.
Check if the car is affordable within a budget of $20000 using the isAffordable() method and print the result using if-else statement.
Determine and print the car’s classification based on its price using if-else-if statement:
If the price is greater than $50000, print “The car is a luxury car.” If the price is greater than $30000, print “The car is a mid-range car.” Otherwise, print “The car is an affordable car.”
public class Car {
private String make;
private String model;
private int year;
private double price;
// Constructor
public Car(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
// Getter methods
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
// Method to determine if the car is affordable or not
public boolean isAffordable(double budget) {
if (price <= budget) {
return true;
} else {
return false;
}
}
public void printCarDetails() {
System.out.println(getMake() +" "+ getModel() +" " + getYear() +" "+ getPrice());
}
// Main method
public static void main(String[] args) {
// Create a new Car object
Car myCar = new Car("Toyota", "Camry", 2019, 25000.0);
// Print the car details
myCar.printCarDetails();
// Check if the car is affordable with a budget of $20000 using an if-else statement
if (myCar.getPrice() < 20000) {
System.out.print("affordable");
}
else System.out.println("not affordable");
// Check if the car is a luxury car based on its price using if-else-if statement
if (myCar.getPrice() > 200000){
System.out.print("luxury car");
}
else if (myCar.getPrice() > 50000){
System.out.print("not a luxury car");
}
System.out.print("regular car");
}
}
Car.main(null);
Hack 4
You are tasked with creating a program that allows a user to enter the number of cars they own, and then input the make and model of each car. The program will then print out the list of cars.
Here are the specific requirements:
- Use a try-catch statement to catch any input/output exceptions that may occur.
- Use a while loop to ensure that the user enters a positive integer for the number of cars they own.
- Use a for loop to iterate over each car and prompt the user to enter the make and model of the car.
import java.util.Scanner;
class Car {
private String make;
private String model;
public Car(String make, String model){
this.make = make;
this.model = model;
}
//getter
public String getMake(){
return make;
}
public String getModel(){
return model;
}
//setter linked to scanner object
public void setMake(String newMake){
this.make = newMake;
}
public void setModel(String newModel) {
this.model = newModel;
}
public static void main(String[] args) {
//input num of car
try{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter number of cars");
int carNum = myObj.nextInt(); //enter integer
} catch (Exception e) {
System.out.println("Error: please enter a number"); //must enter a number otherwise error
}
//create carList
Car[] carList = new Car[carNum]; //carNum = length of carList
Scanner sc = new Scanner(System.in);
while (carNum >= 0){ //carNum must be a positive integer
for(int i=0; i<carList.length; i++){ //iterate each car
carList[i] = new Car();
System.out.println("Enter make of car"); //prompt input make of car
carList[i].setMake(sc.next());
System.out.println();
System.out.println("Enter model of car"); //prompt input model of car
carList[i].setModel(sc.next());
System.out.println();
}
System.out.println("car list:" + Arrays.toString(carList));
}
}
}
Car.main(null);