Units 1-4 hacks
public class Car {
public static void main(String[] args) {
int year = 2023;
double price = 30000.00;
boolean isElectric = true;
System.out.println("Year: " + year);
System.out.println("Price: $" + price);
System.out.println("Is Electric: " + isElectric);
}
}
import java.util.Scanner;
public class CarPrice {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the price of the car: ");
double price = scanner.nextDouble();
System.out.println("The price of the car is $" + price);
scanner.close();
}
}
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);
}
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);
int wheels = cars*4;
System.out.println("wheels: " + wheels);
}
//calculate cars
int wheels = cars*4;
System.out.println("total wheels: " + wheels);
}
}
Hack1.main(null);
public class Car {
// create constructor
private String make;
private String model;
private int year;
private double price;
private boolean isUsed;
public Car(String make2, String model2, int year2, double price2, boolean isUsed2){
make = make2;
model = model2;
year = year2;
price = price2;
isUsed = isUsed2;
}
public String printCarDetails() {
// method to print car details
String return_s = "Make: " + make + " Model: " + model + " year: " + year + " price: " + price + " is it used? " + isUsed;
return return_s;
}
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 );
//ArrayList<Car> cars = new ArrayList<Car>();
Car[] cars = new Car[3];
cars[0] = car1;
cars[1] = car2;
cars[2] = car3;
for (int i=0; i<cars.length; i++){
// String car_print = cars[i].printCarDetails();
System.out.println(cars[i].printCarDetails());
}
}
}
Car.main(null);
public class Car2 {
private String make;
private String model;
private int year;
private double price;
private String result;
// Constructor
public Car2(String make2, String model2, int year2, double price2) {
make = make2;
model = model2;
year = year2;
price = price2;
result = "The car is an affordable car.";
}
// 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 String classification(){
if(price>50000){
result = "The car is a luxury car.";
return result;
}
else if(price>30000){
result = "The car is a mid-range car.";
return result;
}
else{
return result;
}
}
// Main method
public static void main(String[] args) {
// Create a new Car object
Car2 myCar = new Car2("Toyota", "Camry", 2019, 25000.0);
// Print the car details
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
System.out.println("Year: " + myCar.getYear());
System.out.println("Price: " + myCar.getPrice());
// Check if the car is affordable with a budget of $20000 using an if-else statement
System.out.println("Affordable? " + myCar.isAffordable(20000.0));
// Check if the car is a luxury car based on its price using if-else-if statement
System.out.println(myCar.classification());
}
}
Car2.main(null);
import java.util.Scanner;
public class hackCar{
private static int car;
public hackCar(int car2){
car = car2;
}
public static void prompter(){
while(car>0){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Car make: ");
String make = myObj.nextLine(); // Read user input
System.out.println("Make is: " + make); // Output user input
Scanner myObj2 = new Scanner(System.in); // Create a Scanner object
System.out.println("Car model: ");
String model = myObj2
.nextLine(); // Read user input
System.out.println("Model is: " + model); // Output user input
System.out.println();
car--;
}
catch(Exception e) {
System.out.println("Input not valid");
}
}
}
public static void main(String[] args){
hackCar a = new hackCar(5);
a.prompter();
}
}
hackCar.main(null);