Pres. 1 HW
Car Hacks
int cars = 0; // starting number of cars
int numCarsEntering = 2; // number of cars entering at a time
int numIterations = 5; // number of times to loop
int numWheels = 0;
for (int i = 0; i < numIterations; i++) {
cars += numCarsEntering;
numWheels += 8;// use compound operator to add cars
System.out.println("Number of cars on highway: " + cars + " Total wheels on highway: " + numWheels);
}
public class Car {
private String make;
private String model;
private int year;
private double price;
private boolean isUsed;
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;
}
public void printCarDetails() {
System.out.println("Make: " + make + ", Model: " + model + ", Year: " + year + ", Price: $" + price + ", Used: " + isUsed);
}
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla", 2022, 24999.99, false);
Car car2 = new Car("Honda", "Accord", 2018, 18999.99, true);
Car car3 = new Car("Ford", "Mustang", 2020, 34999.99, true);
Car[] cars = new Car[3];
cars[0] = car1;
cars[1] = car2;
cars[2] = car3;
for (int i = 0; i < cars.length; i++) {
cars[i].printCarDetails();
}
}
}
Car.main(null);
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;
}
}
// Main method
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Camry", 2019, 40000.0);
System.out.println(car1.getMake());
System.out.println(car1.getModel());
System.out.println(car1.getYear());
System.out.println(car1.getPrice());
if(car1.isAffordable(20000))
{
System.out.println("Yes, this car is affordable.");
}
else{
System.out.println("No, this car is outside the current budget.");
}
if(car1.price > 50000) {
System.out.println("This is a luxury car.");
} else if(car1.price > 30000) {
System.out.println("This car is a mid-range car.");
} else {
System.out.println("This car is an affordable car.");
}
// Check if the car is a luxury car based on its price using if-else-if statement
}
}
Car.main(null);
import java.util.Scanner;
public class Car {
private int numCars;
// Constructor
public Car(int numCars) {
this.numCars = numCars;
}
// user input
public void scanInput() {
Scanner input = new Scanner(System.in);
while(numCars>0){
for (int i = 0; i < numCars; i++) {
System.out.print("Enter make: ");
String make = input.nextLine(); // Read user input
System.out.println(make);
System.out.print("Enter model: ");
String model = input.nextLine(); // Read user input
System.out.println(model);
}}
}
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("How many cars do you want to own?");
int numCars = sc.nextInt();
if (numCars < 0) {
throw new IllegalArgumentException("Number of cars cannot be negative");
}
Car car1 = new Car(numCars);
car1.scanInput();
} catch (IllegalArgumentException e) {
System.out.println("Must be positive.");
}
}
}
Car.main(null);
import java.util.Scanner;
public class Car {
private int numCars;
// Constructor
public Car(int numCars) {
this.numCars = numCars;
}
// user input
public void scanInput() {
Scanner input = new Scanner(System.in);
for (int i = 0; i < numCars; i++) {
System.out.print("Enter make: ");
String make = input.nextLine(); // Read user input
System.out.println(make);
System.out.print("Enter model: ");
String model = input.nextLine(); // Read user input
System.out.println(model);
}
}
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("How many cars do you want to own?");
int numCars = sc.nextInt();
if (numCars < 0) {
throw new IllegalArgumentException("Number of cars cannot be negative");
}
Car car1 = new Car(numCars);
car1.scanInput();
} catch (IllegalArgumentException e) {
System.out.println("Must be positive.");
}
}
}
Car.main(null);