public class Box extends Object {
private String stuff;
public Box(String stuff) {
this.stuff = stuff;
}
public Box() {
this.stuff = null;
}
public String getStuff() {
return stuff;
}
public void setStuff(String stuff) {
if (this.stuff == null) {
this.stuff = stuff;
return;
}
this.stuff += "|" + stuff;
}
@Override
public String toString() {
int len = stuff.length();
String result = "";
result += drawStars(len) + "\n"
+ ("* " + stuff + " *") + "\n"
+ drawStars(len);
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
if (obj == this) {
return true;
}
Box that = (Box) obj;
return this.stuff == that.stuff;
}
private String drawStars(int len) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < len + 5; i++) {
result.append("*");
}
return result.toString();
}
public static Box merge(Box[] boxes) {
Box result = new Box();
for (Box box : boxes) {
result.setStuff(box.getStuff());
}
return result;
}
public static Box[] split(Box box) {
String[] splitStff = box.getStuff().split("|");
Box[] boxes = new Box[splitStff.length/2 + 1];
int c=0;
for (String stff: splitStff) {
if (!stff.equalsIgnoreCase("|")) {
boxes[c] = new Box(stff);
c++;
}
}
return boxes;
}
public static void main(String[] args) {
Box box = new Box("");
System.out.println(box.getStuff());
System.out.println(box);
box.setStuff("Kitchen");
System.out.println(box);
Box anotherBox = new Box();
System.out.println(box.equals(anotherBox));
anotherBox.setStuff("Kitchen");
System.out.println(box.equals(anotherBox));
Box box1 = new Box("a");
box1.setStuff("x");
Box box2 = new Box("yax");
Box box3 = merge(new Box[]{box1, box2});
System.out.println(box3);
Box box4 = new Box("a|b|c");
Box[] splitBoxes = split(box4);
for (Box splitBox: splitBoxes) {
System.out.println(splitBox);
}
}
}
|
![]() |
Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class....
public class Player { private String name; private int health; public Player(String name) { this.name = name; } } Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.
java
Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...
Assume that you have a String "name" attribute in a Java class definition. Write a public method to return this "name" attribute. public String getName() { return name; } 1.) Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”. 2.) The method getValueFromFile is public and returns an int. It accepts no arguments. The method is capable of throwing an IOException and a FileNotFoundException. Write the header for this method.
public class Pet { //Declaring instance variables private String name; private String species; private String parent; private String birthday; //Zero argumented constructor public Pet() { } //Parameterized constructor public Pet(String name, String species, String parent, String birthday) { this.name = name; this.species = species; this.parent = parent; this.birthday = birthday; } // getters and setters public String getName() { return name; ...
Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...
please write code in java, assignment is due soon Thanks. public interface Named The Named interface is a simple interface which applies to anything which has a name. The only thing required is the following method: String name() which returns the name of the instance. public interface MessageSink, It includes: void deliver(Named sender, String message) delivers the specified message from the given sender. public interface Sharable, which is Named, It includes no method. public abstract class GeneralCapability It incudes: The...
Consider the Automobile class: public class Automobile { private String model; private int rating; // a number 1, 2, 3, 4, 5 private boolean isTruck; public Automobile(String model, boolean isTruck) { this.model = model; this.rating = 0; // unrated this.isTruck = isTruck; } public Automobile(String model, int rating, boolean isTruck) { this.model = model; this.rating = rating; this.isTruck = isTruck; } public String getModel()...
This assignment attempts to model the social phenomenon twitter.
It involves two main classes: Tweet and TweetManager. You will load
a set of tweets from a local file into a List collection. You will
perform some simple queries on this collection.
The Tweet and the TweetManager classes must be in separate files
and must not be in the Program.cs file.
The Tweet Class
The Tweet class consist of nine members that include two static
ones (the members decorated with the...
Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student and “PostGraduate” that inherits form “Graduate”. Write the class definition for the abstract class “Student”. The class definition should include private instance variables of type String to hold the student’s first name, a string for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. Also...
All question base on Java Se
8
Consider the Top class i public abstract class Topí 2 private 3 protected String name; 4 public intx - 12; int age; e public Top(String name)[ this.namename age0; System.out.println(x); 10 12 public abstract void foo(String f); 13 Create a Middle class that extends the Top class. The class should have a single constructor that takes two input parameters: a String (for name) and int (for age). Your constructor should not have any redundant...