JAVA
The following is about creating a class Ruler and testing it.
(i) Create a class Ruler with the attributes brand (which is a string) and length (an integer) which are used to store the brand and length of the ruler respectively. Write a constructor Ruler(String brand, int length) which assigns the brand and length appropriately. Write also the getter/setter methods of the two attributes. Save the content under an appropriate file name. Copy the content of the file as the answers to this part.
(ii) Create another class TestRuler in a separate file with a method main() to test the class Ruler. In main(), create a Ruler object aRuler with brand "Brand A" and length 30. Print the message "A Ruler object has been created: brand is Brand A, length is 30", in which the data "Brand A" and "30" are obtained from the corresponding getter method of the attribute. Run the program. Copy the content of the file and the output showing the message as the answers to this part.
(iii) Create the class StationeryShop which contains an attribute ruler which is a Ruler array. In the constructor StationeryShop(Ruler[] ruler), initialize the attribute ruler using the parameter. Also write the getter/setter method of the attribute ruler. Copy the content of the file as the answers to this part.
(iv) Write a method displayRuler(int index) of the class StationeryShop to produce output similar to the following:
Ruler number 2
brand : Brand A
length : 30
where Brand A is the brand and 30 is the length of ruler, and index has the value 2. The parameter index is the index of the array ruler. Copy the content of the method as the answers to this part.
(v) Add a method brandCount(String aBrand) to the class StationeryShop which returns the number of rulers with brand aBrand. Copy the content of the method as the answers to this part.
(vi) Add another method longRulers() to the class StationeryShop which returns an array with 2 elements containing Ruler objects with longest and second longest lengths, which are assumed to be unique. Copy the content of the method as the answers to this part.
(vii) Create another class TestStationeryShop in a separate file with a method main() to perform the following:
create a StationeryShop object myStationeryShop and initialize it with data for 3 rulers: ("Brand A", 30), ("Brand B", 20), ("Brand C", 60);
display the ruler with index 2 using displayRuler();
print the number of rulers with brand "Brand A";
print the brand and length of the rulers with top 2 lengths;
Ruler.java
public class Ruler {
//member variables
private String brand;
private int length;
//constructor
public Ruler(String brand, int length)
{
this.brand = brand;
this.length = length;
}
//setters
//method that sets brand
public void setBrand(String brand)
{
this.brand = brand;
}
//method that sets length
public void setLength(int length)
{
this.length = length;
}
//getters
//method that gets brand
public String getBrand()
{
return this.brand;
}
//method that gets length
public int getLength()
{
return this.length;
}
}
TestRuler.java
public class TestRuler {
public static void main(String[] args) {
//create a Ruler object with Brand
A and length 30
Ruler aRuler = new Ruler("Brand A",
30);
//display ruler info
System.out.println("A Ruler object
has been created: brand is " + aRuler.getBrand() + ", length is " +
aRuler.getLength());
}
}
Output:

Code Screenshot:


StationeryShop.java
public class StationeryShop {
//attribute
private Ruler[] ruler;
//constructor
public StationeryShop(Ruler[] ruler)
{
this.ruler = ruler;
}
//method that sets the ruler array
public void setRuler(Ruler[] ruler)
{
this.ruler = ruler;
}
//method that returns the ruler array
public Ruler[] getRuler()
{
return this.ruler;
}
//method that displays the ruler info at specified
index
public void displayRuler(int index)
{
System.out.println("Ruler number "
+ index);
System.out.println("brand: " +
ruler[index].getBrand());
System.out.println("length: " +
ruler[index].getLength());
}
//method that return number of brands with given
brand
public int brandCount(String aBrand)
{
int count = 0;
//iterate through the ruler
array
for(int i = 0; i < ruler.length;
i++)
{
//compare and
increment count if match found
if(ruler[i].getBrand().equalsIgnoreCase(aBrand))
count++;
}
return count;
}
//method that returns array of two long rulers
public Ruler[] longRulers()
{
Ruler[] lngRulers = new
Ruler[2];
Ruler temp;
//iterate through the rulers
array
for (int i = 1; i <
this.ruler.length; i++)
{
for (int j = i;
j > 0; j--)
{
//sort the rulers array based on length
if (ruler[j].getLength() > ruler [j -
1].getLength())
{
temp = ruler[j];
ruler[j] = ruler[j -
1];
ruler[j - 1] = temp;
}
}
}
//store the top 2 lengths in the
array
lngRulers[0] = ruler[0];
lngRulers[1] = ruler[1];
return lngRulers;
}
}
TestStationeryShop.java
public class TestStationaryShop {
public static void main(String[] args) {
//create an object to
StationeryShop with 3 rulers
Ruler[] ruler = new Ruler[3];
ruler[0] = new Ruler("Brand A",
30);
ruler[1] = new Ruler("Brand B",
20);
ruler[2] = new Ruler("Brand C",
60);
StationeryShop myStationeryShop =
new StationeryShop(ruler);
//display ruler at index 2
myStationeryShop.displayRuler(2);
//print the number of rulers with
Brand A
System.out.println("\nNumber of
rulers with Brand A: " + myStationeryShop.brandCount("Brand
A"));
//print the brand and length with 2
top lengths
Ruler[] longRulers =
myStationeryShop.longRulers();
System.out.println("\nRulers with
top 2 lengths: ");
//display the brand and length of
top 2 lengths
for(int i = 0; i <
longRulers.length; i++)
{
System.out.println("brand: " + longRulers[i].getBrand() + " length:
" + longRulers[i].getLength());
}
}
}
Output:

Code screenshot:



JAVA The following is about creating a class Ruler and testing it. (i) Create a class...
Java Programming The following is about creating a class Fish and testing it. In every part, correct any syntax errors indicated by NetBeans until no such error messages. (i) Create a class Fish with the attributes name and price. The attributes are used to store the name and the price of the fish respectively. Choose suitable types for them. You can copy the class Counter on p.17 of the unit and modify the content. Copy the content of the file...
JAVA Create a Java project to implement a simple Name class. This class will have the following class variable: First Name, Middle Name, Last Name, and Full Name Create the accessor/getter and mutator/setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Create a main() method to test your project.
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...
Java language
(a) Create a class HexEditor with a constructor which creates a
5x10 text area in a Frame. Also add a pull-down menu with a menu
item "Load". Copy the class as the answer to this part.
(b) Create another class TestHexEditor with a main() method
which creates an object anEditor of the class HexEditor and
displays the frame in part (a) using setVisible(true). Copy the
class as the answer to this part.
(c) Make changes to the class...
JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...
2. Create ONE java file in Dr Java according the specifications below: A. Create a Country class a. Each Country object of the Country class should know (state) i. its name ii. its area (in square miles) iii. its population b. Each Country object should be able to (behavior) i. set initial population (setter) ii. get density (number of people per square mile) (getter) iii. adjust the set population by some amount (setter) iv. provide its name, area, and population...
Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able to store information regarding the name, age, gpa, and phone number of a Student object. Please write all the setter and getter methods. Finally, write a Demo class to demonstrate the use of the class by creating two different objects. 2.Use the same Student class from the previous problem. This time,you will write a different Demo class, in which you will create three Student...
I Need Help with this using Java Programming
:
Class name
fname
lname
Class variable
total Number
Constructor (no arguments)
Constructor (takes two arguments, fname and lname)
One getter method to return the fname and lname
One setter method for fname and lname
Inside Main:
Create three objects with the following
names
Jill Doe
John James
Jack Smith
When creating the first object (Should
not be an anonymous object)
use the argument-less constructor
Then use the setter method to assign...
JAVA I. Using the Event Class created previously, create an array of objects; II. Demonstrate passing array reference to a method; III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...
IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...