Unit 1-4 Hacks - Shraddha Kadaba, Meena Annamalai, Madhumita Narayan
done as a treo
public class Hack1 {
public static void main(String[] args) {
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
for (int i = 0; i < numIterations; i++) {
cars += numCarsEntering; // use compound operator to add cars
System.out.println("Number of cars on highway: " + cars);
}
//print times 4
System.out.println(cars * 4);
}
}
Hack1.main(null);
public class Car {
private String name;
private int year;
private double price;
private boolean isUsed;
private String make;
private String model;
private int budget = 20000;
public Car(String make, int year, String model, double price, boolean isUsed) {
//this.name = name;
this.year = year;
this.price = price;
this.isUsed = isUsed;
this.model = model;
this.make = make;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getYear() {
return year;
}
public boolean getIsUsed() {
return isUsed;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public boolean isAffordable(){
if (price <= budget) {
return true;
} else {
return false;
}
}
public void printCarDetails() {
System.out.println("Make = " + this.getMake() + ", Model = " + this.getModel() + ", Year = " + this.getYear() + ", " + "Price = " + this.getPrice() + " Used car? " + this.getIsUsed());
}
public static void main(String[] args) {
Car[] listOfCars = new Car[3];
Car Toyota = new Car("Toyota", 2022, "Corolla", 24999.99, false);
Car Honda = new Car("Honda", 2018, "Accord", 18999.99, true);
Car Ford = new Car("Ford", 2020, "Mustang", 34999.99, true);
listOfCars[0] = Toyota;
listOfCars[1] = Honda;
listOfCars[2] = Ford;
for (int i = 0; i < listOfCars.length; i++)
{
listOfCars[i].printCarDetails();
}
//System.out.println("The cheaper car is: " + cheaperCarName);
}
}
Car.main(null);
public static void main(String[] args) {
Car[] listOfCars = new Car[1];
Car Toyota2 = new Car("Toyota", 2019, "Camry", 25000.0, false);
listOfCars[0] = Toyota2;
for (int i = 0; i < listOfCars.length; i++)
{
listOfCars[i].printCarDetails();
}
//isAfordable method defined in Hack 2 Code
System.out.println("This car is within the budget of 20000: " + Toyota2.isAffordable());
if (Toyota2.getPrice() > 50000) {
System.out.println("This car is a luxury car");
}
else if (Toyota2.getPrice() > 30000) {
System.out.println("This car is a mid-range car");
}
else {
System.out.println("This car is an affordable car");
}
}
main(null);
import java.util.Scanner;
public class Car2 {
private String make;
private String model;
public Car2(String make, String model) {
this.make = make;
this.model = model;
}
public void displayCarInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
}
public static void main(String[] args) {
try {
Scanner myObj = new Scanner(System.in);
System.out.print("Enter number of cars you own: ");
int numCars = myObj.nextInt();
System.out.println(numCars);
myObj.nextLine();
Car2[] listOfCars2 = new Car2[numCars];
while (numCars > 0) {
for (int i = 0; i < numCars; i++){
System.out.print("Enter make of car for car" + (i+1) + ": ");
String carMake = myObj.nextLine();
System.out.println(carMake);
System.out.print("Enter model of car for car" + (i + 1) + ": ");
String carModel = myObj.nextLine();
System.out.println(carModel + "\n");
Car2 car = new Car2(carMake, carModel);
listOfCars2[i] = car;
}
break;
}
for (int i = 0; i < listOfCars2.length; i++)
{
System.out.println("Car " + (i+1) + ": ");
listOfCars2[i].displayCarInfo();
System.out.println("\n");
}
} catch (InputMismatchException e1) {
System.out.println("Error in input type!");
}
}
}
Car2.main(null);