Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image

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 ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Applying Communication Theory For Professional Life A Practical Introduction

Authors: Marianne Dainton, Elaine D. Zelley

4th Edition

150631547X, 978-1506315478

More Books

Students also viewed these Programming questions

Question

What is demand management?

Answered: 1 week ago

Question

Show that if A is any m n matrix, then Im A = A and AIn = A.

Answered: 1 week ago