Question

JAVA

Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (flo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//creating class Pen
public class Pen {
   //creating private instance variable
private float price;
private String color;
//creating constructor with two fields
public Pen(float price, String color) {
   //if price is negative then it throw IllegalArgumentException
   if(price<0)
   {
       throw new IllegalArgumentException("Erorr :price can not be negative");
   }
   else {
       this.price = price;
   }
  
   this.color = color;
}
//genrating getters and setters methods
public float getPrice() {
   return price;
}
public void setPrice(float price) {
   //if price is negative then it throw IllegalArgumentException
   if(price<0)
   {
       throw new IllegalArgumentException("Erorr :price can not be negative");
   }
}
public String getColor() {
   return color;
}
public void setColor(String color) {
   this.color = color;
}
//tostring method for printing the object data
public String toString() {
   return "Pen [price=" + price + ", color=" + color + "]";
}
//main method for execution
public static void main(String[] args) {
   //creating object of class Pen
   Pen pen=new Pen(-1,"blue");//this line throw exception
   Pen pen1=new Pen(22, "Red");//this will executed
   System.out.println(pen);
   System.out.println(pen1);
}

}
Output-

terminated> Pen [Java Application] C:\Program Files\Java\jre 1.8.O_251\bin\javaw.exe (Oct 15, 2020, 12:58:10 AM – 12:58:12 AM

Add a comment
Know the answer?
Add Answer to:
JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java please Tools Help Mode Delay Write a class called Envelope that contains the following...

    in java please Tools Help Mode Delay Write a class called Envelope that contains the following information: 1. Private instance variables for the height and width of the envelope (int). 2. A two-argument constructor to set each of the instance variables above. If the height or width is negative, throw an llegalArgumentException sta the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and...

    The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and calories (int), along with get/set methods for both. Implement the methods in the Meal class found in Meal.java public class FoodItem{ private String name; private int calories;    public FoodItem(String iName, int iCals){ name = iName; calories = iCals; }    public void setName(String newName){ name = newName; }    public void setCalories(int newCals){ calories = newCals; }    public int getCalories(){ return calories;...

  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • A JavaBean, or bean, is a Java class that A. doesn’t provide get and set methods...

    A JavaBean, or bean, is a Java class that A. doesn’t provide get and set methods for all of it’s private instance variables that follow standard Java naming conventions B. doesn’t provide a zero-argument constructor C. declares public instance variables D. None of the above

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT