Question

In: Computer Science

Write a class called Pen that contains the following information: Private instance variables for the price...

Write a class called Pen that contains the following information:

  1. Private instance variables for the price of the pen (float) and color of the pen (String).
  2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct.
  1. Get and Set methods for each instance variable with the same error detection as the constructor.

public class Pen {

Solutions

Expert Solution

Here is the solution,

// your code starts here
import java.util.Scanner;

public class Pen
{
// instance variables - replace the example below with your own
private float price;
private String color;
  

// constructor with two arguments/parameters
public Pen(float price , String color)
{
try {
if(price < 0)
throw new IllegalArgumentException("Price cannot be negative");
this.price = price;
this.color = color;
}
catch(IllegalArgumentException e) {
System.out.println(e);
}
}
  
// for getting price
// no need for exception since we will never allow negative number
// for the object
public float getPrice()
{
return this.price;
}
  
  
// for getting color
// exception handling is not for color
public float getColor()
{
return this.price;
}
  
  
// setting price, over here we again have to check for negative numbers
// so we have the Exceptional handling (IllegalArgumentException)
public void setPrice(float price)
{
try {
if(price < 0)
throw new IllegalArgumentException("Price cannot be negative");

this.price = price;
  
}
catch(IllegalArgumentException e) {
System.out.println(e);
}

}
  
  
// to set color. No exception handling to be done for color.
public void setColor(String color)
{   
this.color = color;
}
  
  
// main/driver
public static void main(String[] args) {
// trying to create object with negative number
// exception will be thrown here
Pen a = new Pen(-2,"blue");
// creating object
Pen b = new Pen(2,"black");
// trying to set value with negative number
// exception will be thrown here
b.setPrice(-9);
}
}
//code ends here

here is the sample output:-

hhere is the screenshot of the code for proper understanding.

Thank You.


Related Solutions

Using C# Create a class called Artist that contains 4 pieces of information as instance variables:...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables: name (datatype string), specialization – i.e., music, pottery, literature, paintings (datatype string), number of art items (datatype int), country of birth (datatype string). Create a constructor that initializes the 4 instance variables. The Artist class must contain a property for each instance variable with get and set accessors. The property for number should verify that the Artist contributions is greater than zero. If a...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT