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...
(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...
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...
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)...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT