create test cases
class Flight {
private String flightNo;
private Calendar departureTime;
private Calendar arrivalTime;
public Flight(String flightNo, Calendar departureTime,
Calendar arrivalTime) {
this.flightNo = flightNo;
this.departureTime = departureTime;
this.arrivalTime = arrivalTime;
}
public int getFlightTime() {
return (int)(arrivalTime.getTimeInMillis() - departureTime.getTimeInMillis())
/ (1000 * 60);
}
public Calendar getDepartureTime() {
return departureTime;
}
public Calendar getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(Calendar arrivalTime) {
this.arrivalTime = arrivalTime;
}
public void setDepartureTime(Calendar departureTime) {
this.departureTime = departureTime;
}
}
class Itinerary {
private List<Flight> flights;
public Itinerary(List<Flight> flights) {
this.flights = flights;
}
public int getTotalTravelTime() {
int totalTime = getTotalFlightTime();
for (int i = 0; i < flights.size() - 1; i++) {
long time = flights.get(i + 1).getDepartureTime().getTimeInMillis() -
flights.get(i).getArrivalTime().getTimeInMillis();
totalTime += (int)time / (1000 * 60);
}
return totalTime;
}
public int getTotalFlightTime() {
int flightTime = 0;
for (Flight flight: flights)
flightTime += flight.getFlightTime();
return flightTime;
}
}
Please find the test cases I have included in the test class (in bold).
CODE
===========================
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
class Flight {
private String flightNo;
private Calendar departureTime;
private Calendar arrivalTime;
public Flight(String flightNo, Calendar departureTime,
Calendar arrivalTime) {
this.flightNo = flightNo;
this.departureTime = departureTime;
this.arrivalTime = arrivalTime;
}
public int getFlightTime() {
return (int)(arrivalTime.getTimeInMillis() - departureTime.getTimeInMillis())
/ (1000 * 60);
}
public Calendar getDepartureTime() {
return departureTime;
}
public Calendar getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(Calendar arrivalTime) {
this.arrivalTime = arrivalTime;
}
public void setDepartureTime(Calendar departureTime) {
this.departureTime = departureTime;
}
}
class Itinerary {
private List<Flight> flights;
public Itinerary(List<Flight> flights) {
this.flights = flights;
}
public int getTotalTravelTime() {
int totalTime = getTotalFlightTime();
for (int i = 0; i < flights.size() - 1; i++) {
long time = flights.get(i + 1).getDepartureTime().getTimeInMillis() -
flights.get(i).getArrivalTime().getTimeInMillis();
totalTime += (int)time / (1000 * 60);
}
return totalTime;
}
public int getTotalFlightTime() {
int flightTime = 0;
for (Flight flight: flights)
flightTime += flight.getFlightTime();
return flightTime;
}
}
// Test class
public class ItenaryTest {
// driver method
public static void main(String args[]) {
// Create an instance of Itinerary
Itinerary itinerary;
// Test Case1
List<Flight> flights = new ArrayList<>();
Calendar arrival, departure;
arrival = Calendar.getInstance();
departure = Calendar.getInstance();
// creating flight 1
departure.set(2018, 3, 18, 11, 00, 00);
arrival.set(2018, 3, 19, 6, 30, 00);
Flight f1 = new Flight("6E433", departure, arrival);
// creating flight 2
departure.set(2018, 3, 20, 5, 45, 30);
arrival.set(2018, 3, 21, 23, 05, 45);
Flight f2 = new Flight("6E434", departure, arrival);
// creating flight 3
departure.set(2018, 4, 22, 12, 20, 15);
arrival.set(2018, 3, 22, 15, 56, 35);
Flight f3 = new Flight("6E435", departure, arrival);
// adding flight 1, 2 and 3 to the Itinerary
flights.add(f1);
flights.add(f2);
flights.add(f3);
itinerary = new Itinerary(flights);
System.out.println("Test Case 1: Total Travel time = " + itinerary.getTotalFlightTime() + " seconds.");
// Test case 2
// creating flight 1
departure.set(2018, 4, 21, 10, 05, 00);
arrival.set(2018, 3, 21, 17, 15, 05);
f1 = new Flight("6E433", departure, arrival);
// creating flight 2
// In this case, I am knowingly setting the hour hand to 25 (> 24) to see if the code produce erroneous results or not.
departure.set(2018, 3, 21, 25, 45, 30);
arrival.set(2018, 3, 22, 11, 30, 320);
f2 = new Flight("6E434", departure, arrival);
// adding flight 1, 2 and 3 to the Itinerary
flights = new ArrayList<>();
flights.add(f1);
flights.add(f2);
itinerary = new Itinerary(flights);
System.out.println("Test Case 2: Total Travel time = " + itinerary.getTotalFlightTime() + " seconds. (Giving erroneous output!!!");
// Test case 3
// creating flight 1
departure.set(2018, 4, 21, 10, 05, 00);
arrival.set(2018, 3, 21, 17, 15, 05);
f1 = new Flight("6E433", departure, arrival);
// creating flight 2
// In this case, I am knowingly setting the departure time before arrival time
departure.set(2018, 3, 21, 25, 45, 30);
arrival.set(2018, 3, 20, 10, 30, 320);
f2 = new Flight("6E434", departure, arrival);
// adding flight 1, 2 and 3 to the Itinerary
flights = new ArrayList<>();
flights.add(f1);
flights.add(f2);
itinerary = new Itinerary(flights);
System.out.println("Test Case 3: Total Travel time = " + itinerary.getTotalFlightTime() + " seconds. (Giving erroneous output!!!");
}
}
OUTPUT
===========================
create test cases class Flight { private String flightNo; private Calendar departureTime; private Calendar arrivalTime; ...
JAVA ONLY Design two classes: Flight and Itinerary. The Flight class stores the information about a flight with the following members: 1. A data field named flightNo of the String type with getter method. 2. A data field named departureTime of the GregorianCalendar type with getter and setter methods. 3. A data field named arrivalTime of the GregorianCalendar type with getter and setter methods. 4. A constructor that creates a Flight with the specified number, departureTime, and arrivalTime. 5. A...
4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...
public class Car { /* four private instance variables*/ private String make; private String model; private int mileage ; private int year; // /* four argument constructor for the instance variables.*/ public Car(String make) { super(); } public Car(String make, String model, int year, int mileage) { super(); this.make = make; this.model...
How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...
Assume Doctor Class is Following:
class Doctor
{
private String fullName;
private String registryNumber;
private String specialty;
public Doctor(String fullName, String registryNumber, String
specialty)
{
this.fullName = fullName;
this.registryNumber = registryNumber;
this.specialty = specialty;
}
public String getName()
{
return fullName;
}
public String getRegistryNumber()
{
return registryNumber;
}
public String getSpecialty()
{
return specialty;
}
public void setName(String fullName)
{
this.fullName = fullName;
}
public boolean equals(Doctor other)
{
if(registryNumber == other.registryNumber)
return...
How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...
public class Song {
private String title;
private String artist;
private int duration;
public Song() {
this("", "", 0, 0);
}
public Song(String t, String a, int m, int s) {
title = t;
artist = a;
duration = m * 60 + s;
}
public String getTitle() { return title; }
public String getArtist() { return artist; }
public int getDuration() { return duration; }
public int getMinutes() {
return duration / 60;
}
public int getSeconds() {
return...
public class Animal { private String name; //line 1 private int weight; //line 2 private String getName(){ return name; } //line 3 public int fetchWeight(){ return weight; } //line 4 } public class Dog extends Animal { private String food; //line 5 public void mystery(){ //System.out.println("Name = " + name); //line 6 System.out.println("Food = " + food); //line 7 } } I want to know the super...
class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...