Question

Implement the following class in Java: Class name: People Constructor Summary: public People (Str...

Implement the following class in Java:

Class name: People

Constructor Summary:

public People (String name)

Methods:

public People(String name)

public void setName(String name)

public String getName()

Class name: Truck

Constructor Summary:

public Truck (int licensePlate, int onBoard, People people)

Initially, the truck does not contain any on board the vehicle.

If the number of people on board the vehicle is a negative number, the value of onBoard should be stored as zero.

Methods:

public int getLicensePlate()

Returns license plate of the truck.

public int onBoard()

Returns the number of people currently in the truck.

public List getPeople()

The people currently on board the truck.

public void addPeople()

Adds given person to the truck.

Please write a JUnit test to show functionality.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

People.java

public class People {
    private String name;

    public People(String name){
        this.name = name;
    }

    public void setName( String name ){
        this.name = name;
    }

    public String getName(){
        return name;
    }
}

Truck.java

import java.util.ArrayList;
import java.util.List;

public class Truck {

    private int licensePlate;
    private int onBoard;
    List<People> peopleList = new ArrayList<People>();

    public Truck( int licensePlate, int onBoard, People people ){
        this.licensePlate = licensePlate;

        if(licensePlate < 0 ){
            this.onBoard = 0;
        }
        else {
            this.onBoard = onBoard;
        }
        peopleList.add(people);
    }

    public int getLicensePlate(){
        return licensePlate;
    }

    public int onBoard(){
        return onBoard;
    }

    public List getPeople(){
        return  peopleList;
    }

    public void addPeople( People people){
        peopleList.add(people);
    }

}

TestPeople.java

import org.junit.Test;

import static junit.framework.TestCase.assertEquals;

public class TestPeople {
    People people = new People("JohnSmith");

    @Test
    public void testGetName(){
        assertEquals("JohnSmith",people.getName());
    }

    @Test
    public void testSetName(){
        people.setName("Angelina");
        assertEquals("Angelina", people.getName());
    }
}

TestTruck.java

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static junit.framework.TestCase.assertEquals;

public class TestTruck {
    People people1 = new People("Smith");

    @Test
    public void testGetLicensePlate(){

        Truck truck = new Truck(100,1, people1);
        assertEquals(100,truck.getLicensePlate());
    }

    @Test
    public void testOnBoard(){
        Truck truck1 = new Truck(100,1, people1);
        assertEquals(1,truck1.onBoard());
    }

    @Test
    public void testOnBoardInNegativeSenario(){
        Truck truck = new Truck(-500,5, people1);
        assertEquals(0,truck.onBoard());
    }

    @Test
    public void testGetPeople(){
        Truck truck = new Truck(-500,5, people1);
        People people2 = new People("John");
        truck.addPeople(people2);
        List<People> peopleList = new ArrayList<People>();
        peopleList.add(people1);
        peopleList.add(people2);
        assertEquals(peopleList,truck.getPeople());
    }

}

Output:

TestPeople

Run: TestPeople Tests pased: 2 of 2 tests-Oms C:Program PilesJava jdk1.8.0 201bin java.exe.. ,9 | ⓥTestPeople testGetName ột

TestTruck

Run:TestTrucktestGetLicensePlate and 3 more Tests passed: 4 of 4 t C:Program Files Javaljdk1.8.0_201bin java.exe O ms O ms O

Add a comment
Know the answer?
Add Answer to:
Implement the following class in Java: Class name: People Constructor Summary: public People (Str...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • Implement the following class in Java. Class name: Cakes Constructor Summary: public Leaderboard() - Create a...

    Implement the following class in Java. Class name: Cakes Constructor Summary: public Leaderboard() - Create a leaderboard with no entries, and current number of cakes eaten to 0. Methods: public List getContestants() - Return a list of all the contestant scores in the format (Name:Score). Return an empty list if no entries inside list. Please provide a test to show your implementation works, thank you.

  • Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's...

    Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given in Listing 8.1 in the textbook and in LMS). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double, since it may contain a fractional part) and towing capacity in tons (type double). Give your classes...

  • JAVA QUESTION 16 Write and submit the java source for the following class specification: The name...

    JAVA QUESTION 16 Write and submit the java source for the following class specification: The name of the class is Question_16 Class Question_16 has 3 instance variables: name of type String, age of type int and height of type double. Class Question_16 has the following methods: print - outputs the data values stored in the instance variables with the appropriate label setName - method to set the name setAge - method to set the age setHeight - method to set...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    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...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • *PLEASE HELP ASAP* Pet.java /* * Class Pet * * * */ public class Pet {...

    *PLEASE HELP ASAP* Pet.java /* * Class Pet * * * */ public class Pet { /* * Instance variables */ private String name; private String species; private String parent; private String birthday; /* * Constructors */ public Pet( ) { // This is a null or default constructor } //e end null constructor public Pet( String aName, String aSpecies, String aParent, String aBDay ) { setName( aName ); setSpecies( aSpecies ); setParent( aParent ); setBirthday( aBDay ); } //...

  • Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed...

    Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT