PART 1: GETTING STARTED Create a new Java Project. Create a new Tree class and e
ID: 3754413 • Letter: P
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 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.
Explanation / Answer
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) { name = n; age = a; evergreen = e; } // GETTERS (accessors) and SETTERS (mutators) public String getName() { return name; } public int getAge() { return age; } // PUT YOUR METHOD getEvergreen() HERE public boolean getEvergreen() { return evergreen; } public void setName(String s) { name = new String(s); } public void setEvergreen(boolean e) { evergreen = e; } // PUT YOUR METHOD setAge() HERE public void setAge(int age) { this.age = age; } // 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; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.