Create a class called Planet (e.g. https://en.wikipedia.org/wiki/Solar_System) . It has instance
variables, constructor parameters, and accessor methods for the following data:
- String name
- boolean innerPlanet
- double astronomicalUnitsFromSun
- double earthMasses
Create a method called getPlanetaryData() which returns a String in the exact format of
“Mercury is an inner planet, weighs 0.055 Earth masses, and is 0.4 AU from the Sun.” or
“Jupiter is an outer planet, weighs 318 Earth masses, and is 5.2 AU from the Sun.”, etc….
Create another method with the signature public Boolean isSimilarToEarth() which returns true
if and only if the Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.
Create a Main class with a main method. The main method creates all eight Planet objects for our
solar system, calls each Planet’s getPlanetaryData() method, and also reports in one sentence for
Planet whether the Planet is similar to Earth, in the format of “Jupiter is not similar to Earth.”.
Solution
public class planet {
String name;
boolean innerplanet;
double astronmicalunitfromsun;
double earthmases;
String planet=\"\";
planet(){
}
planet(String name,boolean innerplanet,double astronmicalunitfromsun,double earthmases){
this.name=name;
this.innerplanet=innerplanet;
this.astronmicalunitfromsun=astronmicalunitfromsun;
this.earthmases=earthmases;
if(innerplanet==true)
planet=\"inner\";
else
planet=\"outer\";
}
public String getname(){
return name;
}
public boolean getinnerplanet(){
return innerplanet;
}
public double getastronmicalunitfromsun(){
return astronmicalunitfromsun;
}
public double getearthmases(){
return earthmases;
}
public void getplanetarydata(){
System.out.println(tostring());
}
public String tostring(){
return name+\" is an \"+planet+\" planet, weighs:\" +astronmicalunitfromsun+\" Earth masses,
and is\"+earthmases+\" AU from the Sun\";
}
public boolean issimilartoearth(){
if(astronmicalunitfromsun<1||earthmases<15)
return true;
else
return false;
}
}
import java.util.*;
public class lab0604{
public static void main(String args[]){
planet p=new planet(\"mercury\",false,0.055,0.4);
p.getplanetarydata();
boolean issimilar= p.issimilartoearth();
System.out.println(\" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses.\"+issimilar);
System.out.println();
planet p2=new planet(\"mercury\",false,0.055,0.4);
p2.getplanetarydata();
boolean issimilar2= p2.issimilartoearth();
System.out.println(\" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses.\"+issimilar2);
System.out.println();
planet p3=new planet(\"mercury\",false,0.055,0.4);
p3.getplanetarydata();
boolean issimilar3= p3.issimilartoearth();
System.out.println(\" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses.\"+issimilar3);
System.out.println();
planet p4=new planet(\"jupitar\",true,1.055,2.4);
p4.getplanetarydata();
boolean issimilar4= p4.issimilartoearth();
System.out.println(\" Planet is less than 1.
Andreas Schleicher presents at the launch of What does child empowerment mean...
Create a class called Planet (e.g. httpsen.wikipedia.orgwikiSol.pdf
1. Create a class called Planet (e.g. https://en.wikipedia.org/wiki/Solar_System) . It has instance
variables, constructor parameters, and accessor methods for the following data:
- String name
- boolean innerPlanet
- double astronomicalUnitsFromSun
- double earthMasses
Create a method called getPlanetaryData() which returns a String in the exact format of
“Mercury is an inner planet, weighs 0.055 Earth masses, and is 0.4 AU from the Sun.” or
“Jupiter is an outer planet, weighs 318 Earth masses, and is 5.2 AU from the Sun.”, etc….
Create another method with the signature public Boolean isSimilarToEarth() which returns true
if and only if the Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.
Create a Main class with a main method. The main method creates all eight Planet objects for our
solar system, calls each Planet’s getPlanetaryData() method, and also reports in one sentence for
Planet whether the Planet is similar to Earth, in the format of “Jupiter is not similar to Earth.”.
Solution
public class planet {
String name;
boolean innerplanet;
double astronmicalunitfromsun;
double earthmases;
String planet="";
planet(){
}
planet(String name,boolean innerplanet,double astronmicalunitfromsun,double earthmases){
this.name=name;
this.innerplanet=innerplanet;
this.astronmicalunitfromsun=astronmicalunitfromsun;
this.earthmases=earthmases;
if(innerplanet==true)
planet="inner";
else
planet="outer";
2. }
public String getname(){
return name;
}
public boolean getinnerplanet(){
return innerplanet;
}
public double getastronmicalunitfromsun(){
return astronmicalunitfromsun;
}
public double getearthmases(){
return earthmases;
}
public void getplanetarydata(){
System.out.println(tostring());
}
public String tostring(){
return name+" is an "+planet+" planet, weighs:" +astronmicalunitfromsun+" Earth masses,
and is"+earthmases+" AU from the Sun";
}
public boolean issimilartoearth(){
if(astronmicalunitfromsun<1||earthmases<15)
return true;
else
return false;
}
}
import java.util.*;
public class lab0604{
public static void main(String args[]){
planet p=new planet("mercury",false,0.055,0.4);
3. p.getplanetarydata();
boolean issimilar= p.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar);
System.out.println();
planet p2=new planet("mercury",false,0.055,0.4);
p2.getplanetarydata();
boolean issimilar2= p2.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar2);
System.out.println();
planet p3=new planet("mercury",false,0.055,0.4);
p3.getplanetarydata();
boolean issimilar3= p3.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar3);
System.out.println();
planet p4=new planet("jupitar",true,1.055,2.4);
p4.getplanetarydata();
boolean issimilar4= p4.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar4);
System.out.println();
planet p5=new planet("saturn",false,2.3,0.1);
p5.getplanetarydata();
boolean issimilar5= p5.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar5);
System.out.println();
planet p6=new planet("mars",false,3.055,-0.4);
p6.getplanetarydata();
boolean issimilar6= p6.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar6);
System.out.println();
planet p7=new planet("pluto",false,2.055,0.34);
4. p7.getplanetarydata();
boolean issimilar7= p7.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar7);
System.out.println();
planet p8=new planet("venus",false,0.055,0.4);
p8.getplanetarydata();
boolean issimilar8= p8.issimilartoearth();
System.out.println(" Planet is less than 1AU from the Sun or weighs less than 15 Earth
masses."+issimilar8);
}
}
output:
mercury is an outer planet, weighs:0.055 Earth masses, and is0.4 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
mercury is an outer planet, weighs:0.055 Earth masses, and is0.4 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
mercury is an outer planet, weighs:0.055 Earth masses, and is0.4 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
jupitar is an inner planet, weighs:1.055 Earth masses, and is2.4 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
saturn is an outer planet, weighs:2.3 Earth masses, and is0.1 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
mars is an outer planet, weighs:3.055 Earth masses, and is-0.4 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
pluto is an outer planet, weighs:2.055 Earth masses, and is0.34 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true
venus is an outer planet, weighs:0.055 Earth masses, and is0.4 AU from the Sun
Planet is less than 1AU from the Sun or weighs less than 15 Earth masses.true