Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a Java project called Lab7A and create a Java class called CarMain whi

ID: 3852905 • Letter: 1

Question

1. Create a Java project called Lab7A and create a Java class called CarMain which includes the main method Part A: a. Include a class called Car in the same Java file. This class has following instance variables and instance methods Instance Variables Type: String Color: String Gas per mile: double Tank capacity: double Instance Methods Display car details Calculate number of miles can be travel from the gas in the full tank and return it. Calculate the cost per mile. The method takes the cost per gallon as the input parameter and return the cost per mile b. Create two objects of Car class in main method and assign the following values to each of the instance variables Instance variables Ford Black 0.2 9.5 C2 Toyota Red 0.3 7.5 ype (String) olor (String) Gas per mile (double) ank capacity (double)

Explanation / Answer

1.

class Car
{
private String type;
private String color;
private double gpm;
private double capacity;
  
public Car()
{
this.type = " ";
this.color = " ";
this.gpm = 0.0;
this.capacity = 0.0;
}
  
public Car(String type,String color,double gpm,double capacity)
{
this.type = type;
this.color = color;
this.gpm = gpm;
this.capacity = capacity;
}
public void display()
{
System.out.println(" Type :"+type +" Color : "+color +" Gas Per Mile : "+gpm +" Tank Capacity : "+capacity);
  
}
double calculateMiles()
{
return capacity/gpm;
}
double calculateCostPerMile(double cpg)
{
return calculateMiles() *cpg;
}
  
}
class CarMain
{
   public static void main (String[] args)
   {
       Car c1 = new Car("Ford","Black",0.2,9.5);
       Car c2 = new Car("Toyota","Red",0.3,7.5);
      
       c1.display();
       System.out.println(" Miles travelled with full tank : "+c1.calculateMiles());
       System.out.println(" Cost per mile : "+c1.calculateCostPerMile(6.2));
      
      
       c2.display();
       System.out.println(" Miles travelled with full tank : "+c2.calculateMiles());
       System.out.println(" Cost per mile : "+c2.calculateCostPerMile(8.5));
      
       Car c3 = new Car("Nissan","Yellow",0.25,8.5);
      
       c3.display();
       System.out.println(" Miles travelled with full tank : "+c3.calculateMiles());
       System.out.println(" Cost per mile : "+c3.calculateCostPerMile(5.2));
      
   }
}

Output:

Type :Ford Color : Black Gas Per Mile : 0.2 Tank Capacity : 9.5

Miles travelled with full tank : 47.5

Cost per mile : 294.5

Type :Toyota Color : Red Gas Per Mile : 0.3 Tank Capacity : 7.5

Miles travelled with full tank : 25.0

Cost per mile : 212.5

Type :Nissan Color : Yellow Gas Per Mile : 0.25 Tank Capacity : 8.5

Miles travelled with full tank : 34.0

Cost per mile : 176.8

2.

import java.util.Scanner;

class Employee
{
private int empId;
private String employeeName;
private double basicSalary;
  
public Employee(int empId)
{
this.empId = empId;
  
}
  
public void setEmpId(int empId)
{
this.empId = empId;
}
public int getEmpId()
{
return empId;
}
  
public void setEmpName(String employeeName)
{
this.employeeName = employeeName;
}
public String getEmpName()
{
return employeeName;
}
public void setBasicSalary(double basicSalary)
{
this.basicSalary = basicSalary;
}
public double getSalary()
{
return basicSalary;
}
  
public void displayEmployee()
{
System.out.println(" Employee "+empId +" Name : "+employeeName+ " basic Salary : "+basicSalary);
}
  
public double calculateBonus()
{
return basicSalary*0.01;
}
  
}
class EmployeeMain
{
   public static void main (String[] args)
   {
   String name;
   double bs;
   Scanner scan = new Scanner(System.in);
       Employee[] empArray = new Employee[5];
      
      
   for(int i=0;i<5;i++)
   {
   empArray[i] = new Employee(i+1);
   }
  
  
   for(int i=0;i<5;i++)
   {
       System.out.println(" Enter Employee name : ");
       name = scan.next();
       empArray[i].setEmpName(name);
       System.out.println(" Enter basic salary : ");
       bs = scan.nextDouble();
       empArray[i].setBasicSalary(bs);
   }
   for(int i=0;i<5;i++)
   {
   empArray[i].displayEmployee();
   System.out.println(" Bonus : "+empArray[i].calculateBonus());
   }
}
}

output:

Enter Employee name : John

Enter basic salary : 1200

Enter Employee name : Smith

Enter basic salary : 3450

Enter Employee name : Mathew

Enter basic salary : 1448

Enter Employee name : Clara

Enter basic salary : 2088

Enter Employee name : James

Enter basic salary : 1345

Employee 1
Name : John
basic Salary : 1200.0

Bonus : 12.0

Employee 2
Name : Smith
basic Salary : 3450.0

Bonus : 34.5

Employee 3
Name : Mathew
basic Salary : 1448.0

Bonus : 14.48

Employee 4
Name : Clara
basic Salary : 2088.0

Bonus : 20.88

Employee 5
Name : james
basic Salary : 1345.0

Bonus : 13.450000000000001

3.

class Student
{
  
private String name;
private double marks;
private static int numStudents = 0;
private static double classSum = 0.0;
  
public Student(String name,double marks)
{
this.name = name;
this.marks = marks;
numStudents++;
classSum = classSum + marks;
  
}
  
  
  
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setMarks(double marks)
{
this.marks = marks;
}
public double getMarks()
{
return marks;
}
  
public void displayStudent()
{
System.out.println(" Student Name "+name + " Marks : "+marks);
}
  
  
public static void setNumStudents(int n)
{
numStudents = n;
  
}
public static int getNumStudents()
{
return numStudents;
}
  
public static void setClassSum(int s)
{
classSum = s;
}
public static double getClassSum()
{
return classSum;
}
  
public static void findClassAvg()
{
System.out.println(" Class Average : "+classSum/numStudents);
}
  
}
class StudentMain
{
   public static void main (String[] args)
   {
  
   Student st1 = new Student("Ann",75);
   Student st2 = new Student("Bob",85);
   Student st3 = new Student("Emma",95);
  
   st1.displayStudent();
   st2.displayStudent();
   st3.displayStudent();
  
  
Student.findClassAvg();
  
     
}
}

Output:


Student Name Ann
Marks : 75.0

Student Name Bob
Marks : 85.0

Student Name Emma
Marks : 95.0

Class Average : 85.0

4.

class Vehicle

{
   public static void main (String[] args)
   {
       Car c1 = new Car("Ford","Black",0.2,9.5);
      
       c1.display();
       System.out.println(" Miles travelled with full tank : "+c1.calculateMiles());
       System.out.println(" Cost per mile : "+c1.calculateCostPerMile(6.2));
      
  
      
   }
}

Output:

Type :Ford Color : Black Gas Per Mile : 0.2 Tank Capacity : 9.5

Miles travelled with full tank : 47.5

Cost per mile : 294.5