Question
Here is my code and instructions: I am getting an error, please let me know how to fix it: class Bear { String name; //iniatiation
Here is my code and instructions: I am getting an error, please let me know how to fix it:
class Bear {
String name; //iniatiation
int height;
double speed;
public Bear( String name, int height, double speed ) {
this.name = name; // name assignment
this.height = height; // height assignment
this.speed = speed; // speed assignment
}
public void climbTree() {
System.out.println("Bear "+name+" climbs a tree"); // ui
}
public String toString() {
return name+ height+speed;
}
}
____________________________
import java.util.Scanner;
public class Bears {
private Bear[] myBears; //array of Bear
public Bears(int length) {
this.myBears = new Bear[length];
}
public void makeBears() {
String name;
int height;
double speed;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of bears:"); // user input
int n = Integer.parseInt(input.nextLine());
for (int i = 0; i
System.out.println("Enter name of the bear:"); // user input
name = input.nextLine();
System.out.println("Enter height of the bear (integer):"); // user input
height = Integer.parseInt(input.nextLine());
System.out.println("Enter speed of the bear (double):"); // user input
speed = Double.parseDouble(input.nextLine());
Bear b = new Bear(name, height, speed); // setting
myBears[i] = b;
}
input.close();
}
public void allClimbTree() {
for (Bear b : myBears) {
b.climbTree();
}
}
}
____________________________
public class TestBears { //test class
public static void main(String[] args){
Bears bears = new Bears(5);
bears.makeBears();
bears.allClimbTree();
}
_____________
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Bear.climbTree()" because "b" is null
at Bears.allClimbTree(Bears.java:62)
at TestBears.main(TestBears.java:12)
________
Lab Instructions:
Assume that Bear Objects and Bears Objects are created using the classes below. class Bear ( //instance variables not shown public Bear ( String name, int height, double speed ) { //code not shown} public void climbTree () { //code not shown } public String toString() { //code not shown } } class Bears ( } private Bear [] myBears; public Bears () public void makeBears ( ) { public void allclimbTree () { { public void makeBears () { //array of Bear Part A - Write the Bears constructor below. The Bears constructor must initialize all of the properties of class Bears. public Bears () { PART A PART B PART C Part B - Write the Bears void makeBears () method below. The void makeBears () method will ask for the # of Bears, input the properties for each Bear, instantiate a new Bear, and store that Bear in the myBears array. public void allClimbTree () { Part C - Write the Bears allClimbTree () method below. The allClimbTree () method will make every Bear in class Bears climb a tree.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
The error message indicates that the variable b in the foreach ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started