HW
HW
public class Hack1 {
public static void main(String[] args) {
//calculate cars
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of the 4-wheel cars: ");
double car1 = scanner.nextDouble();
Scanner scanner2 = new Scanner(System.in);
System.out.println("Enter the number of the 2-wheel cars: ");
double car2 = scanner2.nextDouble();
System.out.println();
System.out.println("The total number of wheels: " + (int)(car1*4 + car2*2));
scanner.close();
scanner2.close();
}
}
Hack1.main(null);
public class Car {
private String make;
private String model;
private int year;
private double price;
private boolean isUsed;
public Car(String makeInput, String modelInput, int yearInput, double priceInput, boolean isUsedInput){
make = makeInput;
model = modelInput;
year = yearInput;
price = priceInput;
isUsed = isUsedInput;
}
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;
}
// create constructor
public void printCarDetails() {
System.out.println("Make: " + getMake() + ", Model: " + getModel() + ", Year: " + getYear() + ", Price: " + getPrice() + ", Isused: " + getIsUsed());
}
public static void main(String[] args) {
Car[] cars = {new Car("Toyota", "Corolla", 2022, 24999.99, false), new Car("Honda", "Accord", 2018, 18999.99, true), new Car("Ford", "Mustang", 2020, 34999.99, true)};
for(Car num : cars){
num.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;
}
}
public void printCarDetails() {
System.out.println("Make: " + getMake() + ", Model: " + getModel() + ", Year: " + getYear() + ", Price: " + getPrice());
}
// Main method
public static void main(String[] args) {
// Create a new Car object
Car car1 = new Car("Toyota", "Camry", 2019, 25000.0);
// Print the car details
car1.printCarDetails();
// Check if the car is affordable with a budget of $20000 using an if-else statement
if(car1.isAffordable(20000)){
System.out.println("Is affordable: " + car1.isAffordable(20000));
}
else{
System.out.println("Is affordable: " + car1.isAffordable(20000));
}
// Check if the car is a luxury car based on its price using if-else-if statement
if(car1.getPrice() >= 1000000){
System.out.println("Is luxury: " + (car1.getPrice() >= 1000000));
}
else if(car1.getPrice() < 1000000){
System.out.println("Is luxury: " + (car1.getPrice() >= 1000000));
}
}
}
Car.main(null);
public class Car {
private static HashMap<String, Car> Garage = new HashMap<String, Car>();
private String make;
private String model;
private static int carCount;
private long start;
private long finish;
// Constructor
public Car(String make, String model) {
this.make = make;
this.model = model;
this.start = System.currentTimeMillis()/100;
Garage.put((make + " " + model), this);
}
// Getter methods
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public long getCarEnter(){
return start;
}
public long getCarExit(){
finish = System.currentTimeMillis()/100;
return finish;
}
public static void CarExit(String carMakeAndModel){
Garage.remove(carMakeAndModel);
}
public static long getCurrentCarTime(Car check){
return (check.getCarExit() - check.getCarEnter());
}
public static int getCarCount(){
return Garage.size();
}
public String toString() {
return ("Make: " + getMake() + ", Model: " + getModel() + ", Vehicle residence time: " + getCurrentCarTime(this) + " days");
}
// Main method
public static void main(String[] args) {
boolean checkUser = true;
while(true){
while (checkUser){
try{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of cars: ");
int carNum = scanner.nextInt();
while(carNum >0){ //Use a while loop to ensure that the user enters a positive integer for the number of cars they own.
Scanner scanner2 = new Scanner(System.in);
System.out.println("Enter the make of your " + carNum + " car: ");
String carMake = scanner2.nextLine();
Scanner scanner3 = new Scanner(System.in);
System.out.println("Enter the model of your " + carNum + " car: ");
String carModel = scanner3.nextLine();
Car car = new Car(carMake, carModel);
carNum--;
}
if (carNum == 0){
checkUser = false;
}
if (carNum < 0){
System.out.println("Try to enter a positive integer");
}
}catch (ArithmeticException e) {
System.out.println("Error: Wrong input, please enter a positive integer!"); // Catching and handling the exception
}
}
Scanner carList = new Scanner(System.in);
System.out.println("Want to check the cars you own in the garage? Type yes or no: ");
String userChose3 = carList.nextLine();
if(userChose3.equals("yes")){
if(Garage.size() == 0){
System.out.println("There is no car in the garage.");
System.out.println();
}
else{
System.out.println("You have " + getCarCount() + " cars in your garage.");
for (Map.Entry entry : Garage.entrySet()) {
System.out.println(entry.getValue());
}
System.out.println();
}
}
Scanner carExit = new Scanner(System.in);
System.out.println("Do you want to get any cars out from the garage? If yes, enter the car make + space + car model, if no, type no: ");
System.out.println();
String userChose2 = carExit.nextLine();
if(userChose2.equals("no") !=true){
Garage.get(userChose2);
System.out.println("The car you chose is: " + userChose2 + ", the vehicle stayed in garage for about :" + getCurrentCarTime(Garage.get(userChose2)) + " days");
System.out.println();
CarExit(userChose2);
System.out.println("You have " + getCarCount() + " cars in your garage.");
for (Map.Entry entry : Garage.entrySet()) {
System.out.println(entry.getValue());
}
System.out.println();
}
Scanner scanner4 = new Scanner(System.in);
System.out.println("Want to add more car? Enter yes or no: ");
String userChose = scanner4.nextLine();
if (userChose.equals("yes")){
System.out.println();
checkUser = true;
}
else{
break;
}
}
}
}
Car.main(null);
public class CarLab { // CarLab is a class that represents a car
private Long id;
private String name;
// Hack: add columns for other attributes, (ex total mileage, model, make of car) demonstrating knowledge of different data types
private String model;
private String make;
private double mpg;
private double acceleration;
private double price;
public CarLab(String name, double price, String carModel, String carMake, double carMPG) {
this.name = name;
this.price = price;
this.model = carModel;
this.make = carMake;
this.mpg = carMPG;
}
public double getMPG(){
return mpg;
}
public double getPrice(){
return price;
}
public double getAcceleration(){
return acceleration;
}
// Hack: demonstrate another operation on a different data type
public static void acceleration(CarLab car, double s1, double s2, double time) {
car.acceleration = (Math.abs((int) ((s2 - s1) / time)));
}
public static double calTime(CarLab car, int acceleration){
//math kinematic equation, x = Vit + (1/2)at^2
//calculate the time it takes to run out mileage
return Math.sqrt((2*car.getMPG())/acceleration);
}
public static boolean meetBudget(CarLab car, double budget){
return (car.getPrice() < budget);
}
public static boolean meetMPG(CarLab car, double carMPGGoal){
return (car.getMPG() >= carMPGGoal);
}
public static boolean meetAcceleration(CarLab car, double carAccelerationGoal){
return (car.getAcceleration() >= carAccelerationGoal);
}
public String getName(){
return name;
}
public String toString() {
return( "{" +
"\"name\": " + this.getName() + "," +
"\"price\": " + this.getPrice() +
"}" );
}
public static void main(String[] args) {
CarLab[] carList = {new CarLab("name1", 20000, "model1", "make1", 100000), new CarLab("name2", 300000, "model2", "make2", 2000000)};
CarLab cheapest = carList[0];
for (int i = 0; i < carList.length; i++){
acceleration(carList[i], 0.0, 200.0, 10.0);
if (meetAcceleration(carList[i], 10) && meetBudget(carList[i], 200000) && meetMPG(carList[i], 10000)){
System.out.println("Car" + (i+1) + "meet requirement");
if(cheapest.getPrice() > carList[i].getPrice()){
cheapest = carList[i];
}
}
}
System.out.println(cheapest);
}
}
CarLab.main(null);