Write in-line comments that are necessary. Don't write a comment for what is obvious from the code. There are 2 different programs. (Subject is JAVA PROGRAMMING) First Program: // Main.java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the Carrier: "); String carrier = input.nextLine(); System.out.print("Enter the Type: "); String type = input.nextLine();
Write in-line comments that are necessary. Don't write a comment for what is obvious from the code. There are 2 different programs. (Subject is JAVA PROGRAMMING)
First Program:
// Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Carrier: ");
String carrier = input.nextLine();
System.out.print("Enter the Type: ");
String type = input.nextLine();
System.out.print("Enter the Speed: ");
double speed = Double.parseDouble(input.nextLine());
System.out.print("Enter the Memory: ");
double memory = Double.parseDouble(input.nextLine());
System.out.print("Enter the Number Of Apps: ");
int numApps = Integer.parseInt(input.nextLine());
CellPhone p1 = new CellPhone(carrier, type, speed, memory, numApps);
System.out.println("\nCarrier = " + p1.getCarrier());
System.out.println("Type of phone = " + p1.getType());
System.out.println("Speed of phone = " + p1.getSpeed() + " Ghz");
System.out.println("Memory = " + p1.getMemory() + " Gb");
System.out.println("Number of Apps = " + p1.getNumApps());
}
}
//Cellphone.java
public class CellPhone {
private String carrier, type;
private double speed, memory;
private int numApps;
/*
* parameterized constructor
*
* @param carrier
* @param type
* @param speed
* @param memory
* @param numApps
*/
public CellPhone(String carrier, String type, double speed, double memory, int numApps) {
this.carrier = carrier;
this.type = type;
this.speed = speed;
this.memory = memory;
this.numApps = numApps;
}
/*
* default constructor
*/
public CellPhone() {
}
/*
* @return the carrier
*/
public String getCarrier() {
return carrier;
}
/*
* @param carrier the carrier to set
*/
public void setCarrier(String carrier) {
this.carrier = carrier;
}
/*
* @return the type
*/
public String getType() {
return type;
}
/*
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/*
* @return the speed
*/
public double getSpeed() {
return speed;
}
/*
* @param speed the speed to set
*/
public void setSpeed(double speed) {
this.speed = speed;
}
/*
* @return the memory
*/
public double getMemory() {
return memory;
}
/*
* @param memory the memory to set
*/
public void setMemory(double memory) {
this.memory = memory;
}
/*
* @return the numApps
*/
public int getNumApps() {
return numApps;
}
/*
* @param numApps the numApps to set
*/
public void setNumApps(int numApps) {
this.numApps = numApps;
}
}
Main.java is the client program and CellPhone.java is the class file.
Second program:
// Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Vehicle car = new Car(9.4);
Vehicle truck = new Truck(14.1);
Vehicle hybridcar = new HybridCar(3.8);
Vehicle motorcycle = new Motorcycle(6.3);
double litre;
System.out.print("Enter amount of gas in litre: ");
Scanner sc = new Scanner(System.in);
litre = sc.nextDouble();
System.out.println("On " + litre + "L of gas the vehicle can drive: \n");
System.out.println("Car= " + car.getDistance(litre) + "KM");
System.out.println("Truck= " + truck.getDistance(litre) + "KM");
System.out.println("Hybrid Car= " + hybridcar.getDistance(litre) + "KM");
System.out.println("Motorcycle= " + motorcycle.getDistance(litre) + "KM");
sc.close();
}
}
// Car.java
public class Car extends Vehicle {
public Car(double mileage) {
super(mileage);
}
}
// HybridCar.java
public class HybridCar extends Vehicle {
public HybridCar(double mileage) {
super(mileage);
}
}
// Motorcycle.java
public class Motorcycle extends Vehicle {
public Motorcycle(double mileage) {
super(mileage);
}
}
// Truck.java
public class Truck extends Vehicle {
public Truck(double mileage) {
super(mileage);
}
}
// Vehicle.java
public class Vehicle {
private double mileage;
public Vehicle(double mileage) {
this.mileage = mileage;
}
public double getDistance(double litre) {
return (litre * 100) / mileage;
}
}
Main.java is the client program. Class files are Car.java, HybridCar.java, Motorcycle.java, Truck.java, and Vehicle.java.
Step by step
Solved in 7 steps with 11 images