Question

In: Computer Science

Please use java to answer the below question. (i) Create a class Pencil with the attributes...

Please use java to answer the below question.

(i) Create a class Pencil 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 Pencil (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.

(ii) Create another class TestPencil in a separate file with a method main() to test the class Pencil. In main(), create a Pencil object aPencil with brand "Brand A" and length 30. Print the message "A Pencil 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.

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

(iv) Write a method displayPencil(int index) of the class StationeryShop to produce output similar to the following:

Pencil number 2 brand :

Brand A

length : 30

where Brand A is the brand and 30 is the length of pencil, and index has the value 2. The parameter index is the index of the array pencil. Copy the content of the method as the answers.

(v) Add a method brandCount(String aBrand) to the class StationeryShop which returns the number of pencils with brand aBrand. Copy the content of the method as the answers.

(vi) Add another method longRulers() to the class StationeryShop which returns an array with 2 elements containing Pencil objects with longest and second longest lengths, which are assumed to be unique. Copy the content of the method as the answers.

(vii) Create another class TestStationeryShop in a separate file with a method main() to perform the following: 1. create a StationeryShop object myStationeryShop and initialize it with data for 3 rulers: ("Brand A", 30), ("Brand B", 20), ("Brand C", 60); 2. display the ruler with index 2 using displayRuler(); 3. print the number of rulers with brand "Brand A"; 4. print the brand and length of the rulers with top 2 lengths; Run the program. Copy the content of the class and the output as the answers.

Solutions

Expert Solution

Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !

Note: The question is having some mistakes, instead of Ruler it should be Pencil.

===========================================================================

public class Pencil {

    private String brand;
    private int length;

    public Pencil(String brand, int length) {
        this.brand = brand;
        this.length = length;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}

===============================================================

public class TestPencil {

    public static void main(String[] args) {


        Pencil aPencil = new Pencil("Brand A",30);
        System.out.println("A Pencil object has been created "+aPencil.getBrand()
        +" and length "+aPencil.getLength());
    }
}

===============================================================

public class StationeryShop {

    private Pencil pencils[];

    public StationeryShop(Pencil[] pencils) {
        this.pencils = pencils;
    }

    public Pencil[] getPencils() {
        return pencils;
    }

    public void setPencils(Pencil[] pencils) {
        this.pencils = pencils;
    }

    public void displayPencil(int index) {
        if (0 <= index && index < pencils.length) {
            System.out.println("Pencil number " + index + " brand: ");
            System.out.println("Brand " + pencils[index].getBrand());
            System.out.println("Length: " + pencils[index].getLength());
        }
    }

    public int brandCount(String aBrand) {

        int count = 0;
        for (int i = 0; i < pencils.length; i++) {
            if (pencils[i] != null && pencils[i].getBrand().equals(aBrand)) {
                count += 1;
            }
        }
        return count;
    }

    public Pencil[] longPencils() {

        Pencil longestPencil = pencils[0];
        for (int i = 0; i < pencils.length; i++) {

            if (pencils[i] != null && longestPencil.getLength() < pencils[i].getLength()) {
                longestPencil = pencils[i];
            }
        }

        Pencil secondLongest = pencils[0];

        for (int i = 0; i < pencils.length; i++) {
            if (pencils[i] != null && longestPencil.getLength() < pencils[i].getLength()
                    && !secondLongest.getBrand().equals(longestPencil.getBrand())) {
                secondLongest = pencils[i];
            }
        }

        return new Pencil[]{longestPencil, secondLongest};

    }
}

===============================================================

public class TestStationeryShop {

    public static void main(String[] args) {


        StationeryShop myStationeryShop = new StationeryShop(

                new Pencil[]{
                        new Pencil("Brand A",30),
                        new Pencil("Brand B",20),
                        new Pencil("Brand C",60)
                }

        );

        myStationeryShop.displayPencil(2);
        System.out.println("Number of pencils with Brand A: "+myStationeryShop.brandCount("Brand A"));

        Pencil[] longest = myStationeryShop.longPencils();

        System.out.println("First Longest Pencil: "+longest[0].getBrand()+", Length: "+longest[0].getLength());
        System.out.println("Second Longest Pencil: "+longest[1].getBrand()+", Length: "+longest[1].getLength());

    }
}

===============================================================


Related Solutions

java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
Class data (hours on electronics) are below.. please use data fto answer question... 1. A national...
Class data (hours on electronics) are below.. please use data fto answer question... 1. A national survey conducted by Common Sense Media in early 2017 found that the average amount of time in front of various electronic devices was 6 hours and 40 minutes. I believe that this actual number of hours is less than this. Using our class data, at α=.05 is there enough evidence to support my claim? Hours on electronics 6,6.5,4,1,8,8,0.5,4,4,9,4,3,3,2,4,4,3,3,2,2,3,4,1,6,3,5,8,3,2,4,6,4,4,5,6,4,1,1,13,0.75,10,2,9,6,9,6,5,2,3.5,8,4,3,4,2,1.5,6,4,2,3,6,10,8,4,2,4,3,4,7,6,8,4.5,5,2,7,12,3,4.5,11,8,6,5,8,7,8,2,6,2,7,5,4,2,2,1,3,4,4,2,2,1,6,4,6,7,9,5,2,7,11,10,3,10,2.5,4.5,5,5,4,8,9,2,14,12,6,18,6,14,12,8,10,15,16,16,15,16,10,15,17,8,17,17,5,3,2,3,4,5,3,4,2,4,6,6,4,9,5,10,5,9,2,8,12,5,1,5,6,8,5,10,8,10,6,5,10,2,3,10,2,3.5,2.5,2,5,4,0,4,2,2,6,7,1,3.
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to do fraction arithmetic. To get you used to the idea of unit testing, this homework does not require a main method. You can create one if you find it useful, however, we will not be grading or even looking at that code. You should be comfortable enough with the accuracy of your test cases that you do not need to use print statements or...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP a. Create a class named Student that has fields...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP a. Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method...
please i want it with report full answer please Use either multithreading (Java or OpenMP) libraries...
please i want it with report full answer please Use either multithreading (Java or OpenMP) libraries to parallelise the following: 1) Write a program that launches 1,000 threads. Each thread adds 1 to a variable sum that initially is 0. Define a variable to hold sum. - Run the program without synchronization. - Run the program with synchronization in three different techniques 2) Write a program to compute a factorial of a given number in parallel
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a class named Sandwich. Data fields include a String for the main ingredient (such as tuna), a String for bread type (such as wheat), and a double for price (such as 4.99). Include methods to get and set values for each of these fields. Save the class as Sandwich.java. b. Create an application named TestSandwich that instantiates one Sandwich object and demonstrates the use of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT