Question

In: Computer Science

Create a class Pen to represent a pen according to the following requirements: A pen has...

  1. Create a class Pen to represent a pen according to the following requirements:
  1. A pen has two attributes: id and color.
  2. Add a constructer without parameters. In the initialization of the attributes, set the id to zero and the color to an empty string.
  3. Add a constructer with tow parameters to initialize the attributes id and color by a specific values.
  4. Add the method setId that set the id of a pen to a specific value.
  5. Add the method setColor that set the color of a pen to a specific value.
  6. Add the method getId that return the id of a pen.
  7. Add the method getColor that return the color of a pen.

  1. Create the class PenTester with the main method.
  1. Create tow pen P1 and P2 using the first constructor.
  2. Print the characteristics of P1 and P2.
  3. Create one pen P3 using the second constructor.
  4. Change the id of the pen P3.
  5. Print the id of P3.

Solutions

Expert Solution

class Pen {
   private int id;
   private String color;

   public Pen() {
       id = 0;
       color = "";
   }

   public Pen(int aId, String aColor) {
       super();
       id = aId;
       color = aColor;
   }

   /**
   * @return the id
   */
   public int getId() {
       return id;
   }

   /**
   * @param aId
   * the id to set
   */
   public void setId(int aId) {
       id = aId;
   }

   /**
   * @return the color
   */
   public String getColor() {
       return color;
   }

   /**
   * @param aColor
   * the color to set
   */
   public void setColor(String aColor) {
       color = aColor;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Id : " + id + ", Color : " + color;
   }

}

public class PenTester {
   public static void main(String[] args) {
       Pen P1 = new Pen();
       Pen P2 = new Pen();
       System.out.println(P1);
       System.out.println(P2);
       Pen P3 = new Pen(1, "Red");
       P3.setId(2);
       System.out.println("P3 Id : " + P3.getId());
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
For this task you will create a Point3D class to represent a point that has coordinates...
For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class. The Point3D class will have the following state and functionality: Three data fields, x, y and z, of type double, represent the point’s coordinates...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
Requirements:   You are to write a class called Point – this will represent a geometric point...
Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. A copy...
Requirements:   You are to write a class called Point – this will represent a geometric point...
Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. A copy...
Create a class that generates permutations of a set of symbols. Requirements The class must be...
Create a class that generates permutations of a set of symbols. Requirements The class must be named PermutationGenerator. The PermutationGenerator class has two methods as follows. hasNext This method has no parameters.  It returns true if at least one permutation remains to be generated. next This method has no parameters.  It returns an array of the symbols (char[]) in a permutation (if any remain) or null otherwise. The following main method MUST be used, with NO CHANGES to test your class. public static void main(String[] args) { int count = 0; PermutationGenerator pg...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT