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

CS140PROJECT5 Where do the cars meet? This project is worth 40 points. Make sure

ID: 3763148 • Letter: C

Question

CS140PROJECT5 Where do the cars meet? This project is worth 40 points. Make sure to name the classes AutoDemo and Automobile. Submit both files on Moodle by November 20,2015, Ipm. You are given a problem like this: Where do the cars meet? Two cars, an Edsel and a Studebaker, are 635 kilometers apart. They start at the same time and drive towards each other. The Edsel travels at a rate of 70 kilometers per hour and the Studebaker travels 57 kilometers per hour. Where will the two cars meet? Edsel 70km/hr Studebaker -57 km/hr Where do the cars meet? 0 635 Now that you understand the problem, you can solve it. One way to solve it is to create two Automobile objects, and run a simulation where you "drive" them until they cross paths. To do this, you will need to create a class called Automobile. The class is specified by the following UML diagram: Automobile. The classis specified by the following U

Explanation / Answer

Autodemo.java

import java.util.Scanner;
public class Autodemo {
public static void main(String[] args){
  Automobile edsel;
  Automobile studebaker;
  edsel= new Automobile(0,70);
  studebaker =new Automobile(635,-57);
  while(edsel.isWestOf(studebaker)){
   edsel.move();
   studebaker.move();
  }
  System.out.println("The cars meet at edsel="+ edsel.getPosition()+ "Studebaker="+studebaker.getPosition());
  System.out.println("Total distance traveled(all vehicles)=" + Automobile.gettotalDistance());
}

}

Automobile.java:

public class Automobile
{
int MINUTES_PER_HOUR=60;
int defaultTime=1;
static double totaldistance;

double position;
double speed;
double distanceTravelled;
Automobile()
{
  position=0;
  speed=0;
  distanceTravelled=0;
}
Automobile(double origPos)
{
  position=origPos;
}
Automobile(double origPos,double origSpeed)
{
  position=origPos;
  speed=origSpeed;
}
void setPosition(double pos)
{
  position=pos;
}
double getPosition()
{
  return position;
  
}
void setSpeed(double speed)
{
  this.speed=speed;
  
}
double getSpeed()
{
  return speed;
  
}
double getDistanceTravelled()
{
  return distanceTravelled;
  
}
static double gettotalDistance()
{
  
  return totaldistance;
  
}
void move(double time)
{
  
  distanceTravelled =speed*time/MINUTES_PER_HOUR;
  position+=distanceTravelled;
  totaldistance+=distanceTravelled;
  
}
void move()
{
  distanceTravelled =speed*defaultTime/MINUTES_PER_HOUR;
  position+=distanceTravelled;
  totaldistance+=Math.abs(distanceTravelled);
}
boolean isWestOf(Automobile otherAuto)
{
  if(Math.round(this.position)!=Math.round(otherAuto.position))
   return true;
  else
   return false;
}
}

Sample output: