In: Computer Science
Please Use your keyboard (Don't use handwriting) Thank you..
I need new and unique answers, please. (Use your own words, don't copy and paste)
Q1:
Your team is asked to program a self-driving car that reaches its destination with minimum travel time.
Write an algorithm for this car to choose from two possible road trips. You will calculate the travel time of each trip based on the car current speed and the distance to the target destination. Assume that both distances and car speed are given.
Q2:
Write a complete Java program that prints out the following information:
Note:Your program output should look as shown below.
My Name: Ameera Asiri
My student ID: 123456789
My Course Name: Computer Programming
My Course Code: CS140
My Course CRN: 12345
AIYSHA ASIRI
Note : Include the screenshot of the program output as a part of your answer.
_____
Q3:
Write a tester program to test the given class definition.
Create the class named with your id with the main method.
Your answer should be supported with the screenshot of the output, else zero marks will be awarded.
public class Mobile
{
int id;
String brand;
public Mobile()
{
id = 0;
brand="";
}
public Mobile(int n, String name)
{
id = n;
brand=name;
}
public void setBrand(String w)
{
brand = w;
}
public void setId(int w)
{
id = w;
}
public int getId()
{
return id;
}
public String getBrand()
{
return brand;
}
}
___
Q4:
Write a method named raiseSalary that accepts two integers as an argument and return its sum multiplied by 15%. Write a tester program to test the method. The class name should be your ID.
Your answer should be supported with a screenshot of the program with output.
Here is the omplete code for partA with proper comment:
1.
Code:
public class CarTravel
{
//method which select trip with minimum travel time
public static void selectTrip(double dist_A, double dist_B, double
carSpeed)
{
//travel time to destination A
double travelTimeA = dist_A / carSpeed;
double travelTimeB = dist_B / carSpeed;
//print data
System.out.println("Car Take total time to travel destination A: "
+ travelTimeA + " hrs");
System.out.println("Car Take total time to travel destination B: "
+ travelTimeB + " hrs");
System.out.println("Car Decided: ");
//choose the destination Decided by car
if(travelTimeA < travelTimeB)
{
System.out.println("Car will travel to destination A because it
will take less time");
}
else
{
System.out.println("Car will travel to destination B because it
will take less time");
}
}
//execution of program starts from here
public static void main(String[] args) {
//provide the destination of both
roadtrips in kms
double trip_A_dest = 200 ;
//200km
double trip_B_dest = 450 ;
//450km
//provide the car speed
double car_speed = 80;
//80kmph
//method to choose the trip by
car
selectTrip(trip_A_dest,
trip_B_dest, car_speed);
}
}
-----------------------------------------------------------------------------------------------------
Output:
----------------------------------------------------------------------------------------------------------------------
2. Code for Second Part:
Code:
public class Main
{
//execution of program starts from here
public static void main(String[] args) {
//declaring String object and
storing my name
String name = new String("Ameera
Asiri");
//declaring a variable and storing
my id
int id = 123456789;
//other member variables and storing other required
fields
String courseName = "Conputer Programming";
String courseCode = "CS140";
int courseCrn = 12345;
//display all fields
System.out.println("My Name is : " + name);
System.out.println("My student ID : " + id);
System.out.println("My Course Name : " +
courseName);
System.out.println("My Course Code : " +
courseCode);
System.out.println("My Course CRN : " +
courseCrn);
//Replace your first name with your mother / father
name.
name = name.replace("Ameera", "Aiysha");
//make it upperCase
name = name.toUpperCase();
System.out.println(name);
}
}
----------------------------------------------------------------------------------------------------------------------
Output:
-----------------------------------------------------------------------------------------------------------------
3. Complete code:
class Mobile
{
int id;
String brand;
public Mobile()
{
id = 0;
brand="";
}
public Mobile(int n, String name)
{
id = n;
brand=name;
}
public void setBrand(String w)
{
brand = w;
}
public void setId(int w)
{
id = w;
}
public int getId()
{
return id;
}
public String getBrand()
{
return brand;
}
}
//Writing the Tester Code
public class Main //replace this class with yourname + id Example :
class Ameera123456789
{
//execution of program starts from here
public static void main(String[] args) {
//Create two mobiles M1 and M2
using the first constructor.
Mobile M1 = new Mobile();
Mobile M2 = new Mobile();
//Print the characteristics of M1 and M2.
System.out.println("Brand: " + M1.getBrand() + "Id: " +
M1.getId());
System.out.println("Brand: " + M2.getBrand() + "Id: " +
M2.getId());
//Create one mobile M3 using the second constructor.
Mobile M3 = new Mobile(121, "X3");
//Change the id and brand of the mobile M3.
M3.setId(222);
M3.setBrand("Realme");
//Print the id of M3.
System.out.println(M3.getId());
}
}
---------------------------------------------------------------------------------------------------------
Output:
---------------------------------------------------------------------------------------------------------
4. Complete Code:
public class Main
{
//method two calculate Salary
public static double raiseSalary(int sal1, int sal2)
{
//return sum of sal1 + sal2 multiplied by 15%
double totalSalary = (sal1 + sal2) * 0.15; //15% = 15 / 100 =
0.15
return totalSalary;
}
//execution of program starts from here
public static void main(String[] args) {
int sal1 = 200000;
int sal2 = 34000;
//calculate and print Salary
System.out.println(raiseSalary(sal1, sal2));
}
}
--------------------------------------------------------------------------------------
Output: