In: Computer Science
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() );
}
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();
}
}