Question

In: Computer Science

You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....

You need to make an AngryBear class.(In java)

The AngryBear class must have 2 instance variables.

The first instance variable will store the days the bear has been awake.

The second instance variable will store the number of teeth for the bear.

The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth.

The AngryBear class will have one method isAngry();

An AngryBear is angry if it has been awake for more than 3 days and has less than 10 teeth or has no teeth or has been awake for more than 5 days.

Otherwise, the AngryBear is not really angry but he could be quite annoyed.

to test this code use

public void run() {

AngryBear a = new AngryBear( 10, 3 );

System.out.println( a.isAngry() ); //prints true

AngryBear b = new AngryBear( 10, 35 );

System.out.println( b.isAngry() );

AngryBear c = new AngryBear( 1, 25 );

System.out.println( c.isAngry() );

AngryBear d = new AngryBear( 6, 40 );

System.out.println( d.isAngry() );

AngryBear e = new AngryBear( 1, 1 );

System.out.println( e.isAngry() );

AngryBear f = new AngryBear( 111, 111 );

System.out.println( f.isAngry() );

}

Solutions

Expert Solution

Code:

//class is named as Angry Bear
//file should be named as AngryBear.java
class AngryBear{
   //instance variables
   int days;
   int teeth;
   //constructor
   AngryBear(int days,int teeth){
       this.days=days;
       this.teeth=teeth;
   }
   //method
   boolean isAngry(){
       if((days>3 && teeth<10)||(teeth==0)||(days>5)){
           return true;
       }
       else{
           return false;
       }
   }
   //run method
   void run(){
       AngryBear a = new AngryBear( 10, 3 );
       System.out.println( a.isAngry() );
       AngryBear b = new AngryBear( 10, 35 );
       System.out.println( b.isAngry() );
       AngryBear c = new AngryBear( 1, 25 );
       System.out.println( c.isAngry() );
       AngryBear d = new AngryBear( 6, 40 );
       System.out.println( d.isAngry() );
       AngryBear e = new AngryBear( 1, 1 );
       System.out.println( e.isAngry() );
       AngryBear f = new AngryBear( 111, 111 );
       System.out.println( f.isAngry() );

   }

   //main method
   public static void main(String[] args) {
       //creating instances of AngryBear
       AngryBear ab = new AngryBear(2,4);
       //calling run method
       ab.run();
           }
}

Output:

Code Screenshot:

//class is named as Angry Bear
//file should be named as AngryBear.java
class AngryBear{
        //instance variables
        int days;
        int teeth;
        //constructor
        AngryBear(int days,int teeth){
                this.days=days;
                this.teeth=teeth;
        }
        //method
        boolean isAngry(){
                if((days>3 && teeth<10)||(teeth==0)||(days>5)){
                        return true;
                }
                else{
                        return false;
                }
        }
        //run method
        void run(){
                AngryBear a = new AngryBear( 10, 3 );
                System.out.println( a.isAngry() );
                AngryBear b = new AngryBear( 10, 35 );
                System.out.println( b.isAngry() );
                AngryBear c = new AngryBear( 1, 25 );
                System.out.println( c.isAngry() );
                AngryBear d = new AngryBear( 6, 40 );
                System.out.println( d.isAngry() );
                AngryBear e = new AngryBear( 1, 1 );
                System.out.println( e.isAngry() );
                AngryBear f = new AngryBear( 111, 111 );
                System.out.println( f.isAngry() );

        }

        //main method
        public static void main(String[] args) {
                //creating instances of AngryBear
                AngryBear ab = new AngryBear(2,4);
                //calling run method
                ab.run();
                        }
}

Related Solutions

(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...
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...
You have written a JAVA program that creates Shoe object with three instance variables. You will...
You have written a JAVA program that creates Shoe object with three instance variables. You will need to use the exception you created to go along with it. So far you have been using my driver classes. Now it is time for you to create your own driver class that will implement an ArrayList of Shoe objects. This assignment uses the Shoe.java and ShoeException.java to create the driver ShoeStore.Java You will display a menu as follows: Add shoes Print all...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
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 to hold the rectangle’s width....
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 FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables   ...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables    private long accountNumber;    private String firstName;    private String lastName;    private double balance;       //constructor    public Account(long accountNumber, String firstName, String lastName, double balance) {        this.accountNumber = accountNumber;        this.firstName = firstName;        this.lastName = lastName;        this.balance = balance;    }    //all getters and setters    public long getAccountNumber() {        return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT