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

TestTruck

Implement the following class in Java: Class name: People Constructor Summary: public People (Str...
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 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.
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...
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...
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...
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)...
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 */...
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...
Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...
The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and calories (int), along with get/set methods for both. Implement the methods in the Meal class found in Meal.java public class FoodItem{ private String name; private int calories; public FoodItem(String iName, int iCals){ name = iName; calories = iCals; } public void setName(String newName){ name = newName; } public void setCalories(int newCals){ calories = newCals; } public int getCalories(){ return calories;...