Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Find study resources 숿

North South University / CSE / CSE 215 / Plant Flower extends Plant name: String h

Question 숤 Answered step-by-step

Image transcription text

Plant Flower extends Plant name: String hasSmell: boolean color: String hasThorn: boolean /* constructor * / /
* constructor */ /* accessor-mutator */ /* accessor-mutator */ /* toString */ tostring */ Herb extends Plant
isMedicinal: boolean season: String * constructor * / /* accessor-mutator * / * toString */

In main method, create an array of Plant objects and implement these


methods: static void add(Plant [] plants, Plant p) // to add new plant object into
the array static void remove(Plant [] plants, String n) // remove a plant given
its name static Plant search(Plant [] plants, String n) // search for a plant given
its name static void display(Plant [] plants) // display all Plant objects

Computer Science Engineering & Technology Java Programming CSE 215 섈 쉋

Answer & Explanation Solved by veri ed expert 숨 Rated 싙 Helpful

Java code
public class Plant {

private String name;

private String color;

public Plant() {}

public Plant(String name ,String color) {

this.name = name;

this.color = color;

public String getname(){return name;}

private String getcolor(){return color;}

public String toString(){

return "Name "+name+" , Color "+color;


}

public class Flower extends Plant {

private boolean hasSmell;

private boolean hasThorn;

public Flower(boolean hasSmell ,boolean hasThorn) {


super("","");
this.hasSmell = hasSmell;

this.hasSmell = hasSmell;

public boolean gethasSmell(){return hasSmell;}

private boolean gethasThorn(){return hasThorn;}

public String toString(){

return "hasSmell "+hasSmell+" , hasThorn "+hasThorn;


}

}
public class Herb extends Plant {

private boolean Herb;

private String season;

public Herb(boolean Herb ,String season) {


super("","");
this.Herb = Herb;

this.season = season;

public boolean getHerb(){return Herb;}

private String getseason(){return season;}

public String toString(){

return "Herb "+Herb+" , season "+season;


}

}
public class inMain{

static int size = 3;

public static void main(String[] args){

Plant objects[] = new Plant[size];

objects[0] = new Plant("A","red");


objects[1] = new Plant("B","blue");
objects[2] = new Plant("C","white");

display(objects);

System.out.println("remove B");
remove(objects,"B");
display(objects);

System.out.println("add D");

Plant p = new Plant("D", "yellow");


add(objects,p);

display(objects);

static void add(Plant [] plants, Plant p) {

plants[size]=p;
size++;
}

static void remove(Plant [] plants, String n) {

for(int i = 0 ; i < size;i++){

if(plants[i].getname() == n )
plants[i]=plants[i+1];

}
size--;
}

static Plant search(Plant [] plants, String n){

Plant temp =null;


for(int i = 0 ; i < plants.length-1;i++){

if(plants[i].getname() == n ){
temp = plants[i];

}
}

return temp;

static void display(Plant [] plants){

for(int i = 0 ; i < size ;i++){

System.out.println(plants[i]);

}
}

Step-by-step explanation

steps of solution

create class plant and its attributes and getter method ,tostring and constructor
public class Plant {

private String name;

private String color;

public Plant() {}

public Plant(String name ,String color) {

this.name = name;

this.color = color;

public String getname(){return name;}

private String getcolor(){return color;}

public String toString(){

return "Name "+name+" , Color "+color;


}

create class ower and its attributes and getter method ,tostring and constructor
public class Flower extends Plant {

private boolean hasSmell;

private boolean hasThorn;

public Flower(boolean hasSmell ,boolean hasThorn) {


super("","");
this.hasSmell = hasSmell;

this.hasSmell = hasSmell;

public boolean gethasSmell(){return hasSmell;}

private boolean gethasThorn(){return hasThorn;}

public String toString(){

return "hasSmell "+hasSmell+" , hasThorn "+hasThorn;


}

create class herb and its attributes and getter method ,tostring and constructor
public class Herb extends Plant {

private boolean Herb;

private String season;

public Herb(boolean Herb ,String season) {


super("","");
this.Herb = Herb;

this.season = season;

public boolean getHerb(){return Herb;}

private String getseason(){return season;}

public String toString(){

return "Herb "+Herb+" , season "+season;


}

create main method and implement the methods add , remove , search and display
public class inMain{

static int size = 3;

public static void main(String[] args){

Plant objects[] = new Plant[size];

objects[0] = new Plant("A","red");


objects[1] = new Plant("B","blue");
objects[2] = new Plant("C","white");

display(objects);

System.out.println("remove B");
remove(objects,"B");
display(objects);

System.out.println("add D");

Plant p = new Plant("D", "yellow");


add(objects,p);

display(objects);

static void add(Plant [] plants, Plant p) {

plants[size]=p;
size++;
}

static void remove(Plant [] plants, String n) {

for(int i = 0 ; i < size;i++){

if(plants[i].getname() == n )
plants[i]=plants[i+1];

}
size--;
}

static Plant search(Plant [] plants, String n){

Plant temp =null;


for(int i = 0 ; i < plants.length-1;i++){

if(plants[i].getname() == n ){
temp = plants[i];

}
}

return temp;

static void display(Plant [] plants){

for(int i = 0 ; i < size ;i++){

System.out.println(plants[i]);

}
}

output
Name A , Color red
Name B , Color blue
Name C , Color white
remove B
Name A , Color red
Name C , Color white
add D
Name A , Color red
Name C , Color white
Name D , Color yellow

Student review 싙 100% (1 rating)

Is this answer helpful? Helpful 싙 Unhelpful 싗

Add to library

Related Answered Questions

숵 Q: Java Programming in-depth 숵 Q: These are questions from a


discussing in encouraged Compare and chapter I had issues with. It's java
contrast the input vs. output streams.… programming. 1. Consider the followin…

Answered 61d ago 싙 100% Answered 53d ago 싙 100%

숵 Q: Java Programming: Create 숵 Q: I have to use notepad ++ and java


encapsulated Book class, so that it programming (Array list, constructors
abstracts the concept of a book that… and methods to be used) Objectives —…

Answered 59d ago 싙 100% Answered 43d ago 싙 100%

솩 솩

Related Course Resources

CSE 115 CSE 215 CSE 2

North South University North South University North South Un 쇴


섵 823 Documents 섵 723 Documents 섵 614 Docum
❓ 58 Question & Answers ❓ 99 Question & Answers ❓ 52 Questio

솩 솩 솩 솩
Company Get Course Hero Careers

About Us iOS Leadership


Scholarships Android Careers
Sitemap Educators Campus Rep Program
Standardized Tests
Education Summit
Educator Resources

Help Legal Connect with Us

Contact Us Copyright Policy College Life


FAQ Academic Integrity Facebook
Feedback Our Honor Code Twitter
Privacy Policy LinkedIn
Terms of Use YouTube
Instagram

Copyright © 2021. Course Hero, Inc.

Course Hero is not sponsored or endorsed by any college or university.

You might also like