Question
PART 1: GETTING STARTED Create a new Java Project. Create a new Tree class and enter this code. You do not have to type in
PART 1: GETTING STARTED
Create a new Java Project.
Create a new Tree class and enter this code. You do not have to type in the comments.
public class Tree
{
private String name;
private int age;
private boolean evergreen;// true if evergreen
// false if deciduous
// CONSTRUCTORS
public Tree()
{
name = "";
age = 0;
evergreen = true;
}
// you fill this one
public Tree (String n, int a, boolean e)
{
// PUT YOUR CODE HERE
}
Page Break
// GETTERS (accessors) and SETTERS (mutators)
public String getName()
{
return name;
}
public int getAge()
{
// PUT YOUR CODE HERE
}
// PUT YOUR METHOD getEvergreen() HERE
public void setName(String s)
{
name = new String(s);
}
public void setEvergreen(boolean e)
{
// PUT YOUR CODE HERE
}
// PUT YOUR METHOD setAge() HERE
// OTHER METHODS
public String toString()
{
String s = "This " + name + " tree is " + age
+ " years old. ";
if (evergreen)
s = s + "It stays green all year round";
else
s = s + "It loses its leaves in the fall";
return s;
}
}
another question:
Create a SEPARATE class in a SEPARATE file within the same project. Call it Lab5 and enter this code. You do not have to type in the comments.
/*
* This program tests the Tree class
*/
public class Lab5 {
public static void main (String args[])
{
Tree oak = new Tree();
oak.setName("Oak");
oak.setAge(10);
oak.setEvergreen(false);
System.out.println(oak);
Tree pine = new Tree("Pine", 15, true);
System.out.println(pine.getName());
System.out.println(pine.getAge());
System.out.println(pine.getEvergreen());
System.out.println(pine.toString());
}
}
PART 2: WRITING CLASS METHODS
Fill in the code for the second constructor.
Fill in the code for the getAge() method.
Write the entire getEvergreen() method, including the header.
Fill in the code for the setEvergreen() method.
Write the entire setAge() method, including the header.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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