SlideShare a Scribd company logo
1 of 5
Download to read offline
Create an enumerated data type Color for the possible flower colors for a plant (red, purple,
white, none) Create an abstract class Plant with the following features: • class variable
ENERGY_SOURCE whose value is “carbon dioxide” • instance variables for color and watering
• no-argument constructor that sets flower color to white • constructor that takes user-specified
color (both constructors should set watering by calling getRandomWatering()) • getter • setter •
toString() • abstract method getRandomWatering() that returns int for days between watering
Create a Fern class that extends Plant and contains the following: • no-argument constructor that
assigns color value of none (ferns don’t have flowers) • method getRandomWatering() that
returns 7 three out of five times, 10 the rest • toString() Create a Succulent class that extends
Plant and implements Comparable and contains the following: • class variable called
numberOfSucculents to count succulent objects • class method numberOfSucculents() that
returns number of succulents • instance variable hasSpines that is set to true if plant is a cactus,
false otherwise • no-argument constructor that assigns default value of false for hasSpines and
white for color • constructor that assigns user-specified values for hasSpines and color • copy
constructor • getter (call it hasSpines rather than getSpines since it returns a boolean) • setter •
toString() • method getType() that returns “cactus” if hasSpines is true, “succulent” otherwise •
method isSameType() that returns true if both plants are the same subtype • method
getRandomWatering() that returns a random value between 14 and 28 • method compareTo()
that returns 1 if first plant can go more days between waterings, 0 if both same, -1 if first can go
fewer days
Solution
package org.learning.chegg;
public enum Color {
RED, PURPLE, WHITE, NONE;
}
package org.learning.chegg;
public abstract class Plant {
public static String ENERGY_SOURCE = "carbon dioxide";
/*
* Color of the plant
*/
private Color color;
/*
* days between watering
*/
private int watering;
public Plant() {
this.color = Color.WHITE;
this.watering = getRandomWatering();
}
public Plant(Color color){
this.color = color;
this.watering = getRandomWatering();
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getWatering() {
return watering;
}
public void setWatering(int watering) {
this.watering = watering;
}
@Override
public String toString() {
return "Plant[Color: "+this.color+", Watering: "+this.watering+"]";
}
public abstract int getRandomWatering();
}
package org.learning.chegg;
public class Fern extends Plant {
public Fern() {
setColor(Color.NONE);
}
private static int chance = 1;
@Override
public int getRandomWatering() {
if(chance < 4){
chance++;
return 7;
}else{
chance++;
if(chance == 7){
chance = 1;
}
return 10;
}
}
}
package org.learning.chegg;
public class Succulent extends Plant implements Comparable{
private static int numberOfSucculents;
public static int numberOfSucculents(){
return numberOfSucculents;
}
private boolean hasSpines;
public void setHasSpines(boolean hasSpines) {
this.hasSpines = hasSpines;
}
public boolean hasSpines() {
return hasSpines;
}
public String getType(){
if(hasSpines){
return "cactus";
} else{
return "succulent";
}
}
public Succulent() {
this.hasSpines = false;
setColor(Color.WHITE);
}
public Succulent(boolean hasSpine, Color color) {
super(color);
this.hasSpines = hasSpine;
}
@Override
public int getRandomWatering() {
int value = 0;
while(true){
value = (int)(Math.ceil(Math.random() * 100)/2);
if(value > 28 || value < 14){
continue;
}else{
break;
}
}
return value;
}
public boolean isSameType(Succulent succulent){
if(this.getType().equals(succulent.getType())){
return true;
}
return false;
}
@Override
public int compareTo(Succulent o) {
if(this.getWatering() > o.getWatering()){
return 1;
} else if (this.getWatering() == o.getWatering()){
return 0;
} else {
return -1;
}
}
}

More Related Content

More from fazilfootsteps

potential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdfpotential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdf
fazilfootsteps
 
Please write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdfPlease write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdf
fazilfootsteps
 
Module 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdfModule 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdf
fazilfootsteps
 
List and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdfList and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdf
fazilfootsteps
 
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdfIntroduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
fazilfootsteps
 
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdfIndian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
fazilfootsteps
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdf
fazilfootsteps
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
fazilfootsteps
 
Here is the company database for the problem--commen.pdf
Here is the company database for the problem--commen.pdfHere is the company database for the problem--commen.pdf
Here is the company database for the problem--commen.pdf
fazilfootsteps
 
Fundamental Principles of Professional Ethics Part A of the Code Int.pdf
Fundamental Principles of Professional Ethics Part A of the Code Int.pdfFundamental Principles of Professional Ethics Part A of the Code Int.pdf
Fundamental Principles of Professional Ethics Part A of the Code Int.pdf
fazilfootsteps
 
Discuss the differences between a threat and an attack. Describe how.pdf
Discuss the differences between a threat and an attack. Describe how.pdfDiscuss the differences between a threat and an attack. Describe how.pdf
Discuss the differences between a threat and an attack. Describe how.pdf
fazilfootsteps
 
Define an ADT for a sequence of integers (remember that a sequenc.pdf
Define an ADT for a sequence of integers (remember that a sequenc.pdfDefine an ADT for a sequence of integers (remember that a sequenc.pdf
Define an ADT for a sequence of integers (remember that a sequenc.pdf
fazilfootsteps
 

More from fazilfootsteps (20)

Robbinsdale Hospital is one of two hospitals among the six facilitie.pdf
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdfRobbinsdale Hospital is one of two hospitals among the six facilitie.pdf
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdf
 
Research excise taxes in other states besides North Carolina. Report.pdf
Research excise taxes in other states besides North Carolina. Report.pdfResearch excise taxes in other states besides North Carolina. Report.pdf
Research excise taxes in other states besides North Carolina. Report.pdf
 
Prove that E X is not connected if and only if there exist open sets.pdf
Prove that E  X is not connected if and only if there exist open sets.pdfProve that E  X is not connected if and only if there exist open sets.pdf
Prove that E X is not connected if and only if there exist open sets.pdf
 
potential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdfpotential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdf
 
Please write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdfPlease write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdf
 
Nessus is a network security tool- write a pragraph describe itsto.pdf
Nessus is a network security tool- write a pragraph describe itsto.pdfNessus is a network security tool- write a pragraph describe itsto.pdf
Nessus is a network security tool- write a pragraph describe itsto.pdf
 
Module 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdfModule 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdf
 
List and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdfList and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdf
 
Most integer types are directly supported by hardware, but some are n.pdf
Most integer types are directly supported by hardware, but some are n.pdfMost integer types are directly supported by hardware, but some are n.pdf
Most integer types are directly supported by hardware, but some are n.pdf
 
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdfIntroduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
 
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdfIndian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdf
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
 
Here is the company database for the problem--commen.pdf
Here is the company database for the problem--commen.pdfHere is the company database for the problem--commen.pdf
Here is the company database for the problem--commen.pdf
 
Fundamental Principles of Professional Ethics Part A of the Code Int.pdf
Fundamental Principles of Professional Ethics Part A of the Code Int.pdfFundamental Principles of Professional Ethics Part A of the Code Int.pdf
Fundamental Principles of Professional Ethics Part A of the Code Int.pdf
 
Explain why keeping abreast of communications technologies is import.pdf
Explain why keeping abreast of communications technologies is import.pdfExplain why keeping abreast of communications technologies is import.pdf
Explain why keeping abreast of communications technologies is import.pdf
 
estion 49 (1 point) to conomists Barry Bosworth and Susan Collins fou.pdf
estion 49 (1 point) to conomists Barry Bosworth and Susan Collins fou.pdfestion 49 (1 point) to conomists Barry Bosworth and Susan Collins fou.pdf
estion 49 (1 point) to conomists Barry Bosworth and Susan Collins fou.pdf
 
Discuss the differences between a threat and an attack. Describe how.pdf
Discuss the differences between a threat and an attack. Describe how.pdfDiscuss the differences between a threat and an attack. Describe how.pdf
Discuss the differences between a threat and an attack. Describe how.pdf
 
Define an ADT for a sequence of integers (remember that a sequenc.pdf
Define an ADT for a sequence of integers (remember that a sequenc.pdfDefine an ADT for a sequence of integers (remember that a sequenc.pdf
Define an ADT for a sequence of integers (remember that a sequenc.pdf
 
Describe the application of policies and procedures to managing acco.pdf
Describe the application of policies and procedures to managing acco.pdfDescribe the application of policies and procedures to managing acco.pdf
Describe the application of policies and procedures to managing acco.pdf
 

Recently uploaded

Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 

Create an enumerated data type Color for the possible flower colors .pdf

  • 1. Create an enumerated data type Color for the possible flower colors for a plant (red, purple, white, none) Create an abstract class Plant with the following features: • class variable ENERGY_SOURCE whose value is “carbon dioxide” • instance variables for color and watering • no-argument constructor that sets flower color to white • constructor that takes user-specified color (both constructors should set watering by calling getRandomWatering()) • getter • setter • toString() • abstract method getRandomWatering() that returns int for days between watering Create a Fern class that extends Plant and contains the following: • no-argument constructor that assigns color value of none (ferns don’t have flowers) • method getRandomWatering() that returns 7 three out of five times, 10 the rest • toString() Create a Succulent class that extends Plant and implements Comparable and contains the following: • class variable called numberOfSucculents to count succulent objects • class method numberOfSucculents() that returns number of succulents • instance variable hasSpines that is set to true if plant is a cactus, false otherwise • no-argument constructor that assigns default value of false for hasSpines and white for color • constructor that assigns user-specified values for hasSpines and color • copy constructor • getter (call it hasSpines rather than getSpines since it returns a boolean) • setter • toString() • method getType() that returns “cactus” if hasSpines is true, “succulent” otherwise • method isSameType() that returns true if both plants are the same subtype • method getRandomWatering() that returns a random value between 14 and 28 • method compareTo() that returns 1 if first plant can go more days between waterings, 0 if both same, -1 if first can go fewer days Solution package org.learning.chegg; public enum Color { RED, PURPLE, WHITE, NONE; } package org.learning.chegg; public abstract class Plant { public static String ENERGY_SOURCE = "carbon dioxide"; /* * Color of the plant */ private Color color;
  • 2. /* * days between watering */ private int watering; public Plant() { this.color = Color.WHITE; this.watering = getRandomWatering(); } public Plant(Color color){ this.color = color; this.watering = getRandomWatering(); } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public int getWatering() { return watering; } public void setWatering(int watering) { this.watering = watering; } @Override public String toString() { return "Plant[Color: "+this.color+", Watering: "+this.watering+"]"; } public abstract int getRandomWatering(); } package org.learning.chegg; public class Fern extends Plant { public Fern() { setColor(Color.NONE); }
  • 3. private static int chance = 1; @Override public int getRandomWatering() { if(chance < 4){ chance++; return 7; }else{ chance++; if(chance == 7){ chance = 1; } return 10; } } } package org.learning.chegg; public class Succulent extends Plant implements Comparable{ private static int numberOfSucculents; public static int numberOfSucculents(){ return numberOfSucculents; } private boolean hasSpines; public void setHasSpines(boolean hasSpines) { this.hasSpines = hasSpines; } public boolean hasSpines() { return hasSpines; } public String getType(){
  • 4. if(hasSpines){ return "cactus"; } else{ return "succulent"; } } public Succulent() { this.hasSpines = false; setColor(Color.WHITE); } public Succulent(boolean hasSpine, Color color) { super(color); this.hasSpines = hasSpine; } @Override public int getRandomWatering() { int value = 0; while(true){ value = (int)(Math.ceil(Math.random() * 100)/2); if(value > 28 || value < 14){ continue; }else{ break; } } return value; } public boolean isSameType(Succulent succulent){ if(this.getType().equals(succulent.getType())){ return true; } return false; }
  • 5. @Override public int compareTo(Succulent o) { if(this.getWatering() > o.getWatering()){ return 1; } else if (this.getWatering() == o.getWatering()){ return 0; } else { return -1; } } }