Hello, I need help on program that will allowed me to travel on the space and vi
ID: 3543371 • Letter: H
Question
Hello,
I need help on program that will allowed me to travel on the space and visit four plants.
So I need the program read data about the person who's traveling from the user input.
then calculate the wieght and local age on each of the 4 plants.
Then will print out the data the user input it , and the results for each plants showed the age in that plant and the wieght.
Here some info about the plants:
These info for the wieght ratio in the plant which is the first number. then the second one is the earth days.
Thank you,
Explanation / Answer
######## Planet.java############
package abc;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class Planet
{
private String name;
private double gravity;
private int yearLength;
/** Constructs a Planet
@param planetName is the name of the Planet
@param weightRatio is the ratio of an object's weight on the planet to
that on Earth
@param daysInYear is the number of earth days in the planet's year
*/
public Planet( String planetName, double weightRatio, int daysInYear)
{
name = planetName;
gravity = weightRatio;
yearLength = daysInYear;
}
public double getLocalWeight (double earthWeight)
{
double localweight=this.getWeightRatio()*earthWeight;
return localweight;
}
public double getLocalAge (Date birthdate)
{
GregorianCalendar now = new GregorianCalendar();
Date today = now.getTime();
long earthdays = ((today.getTime())/1000 - (birthdate.getTime())/1000)/(60*60*24);
float age=earthdays/(float)this.yearLength;
return age;
}
public void display(double weight,Date birthdate)
{
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(getName()+" "+df.format(this.getLocalWeight(weight))+" "+df.format(this.getLocalAge(birthdate)));
}
/** Accessor function for the Planet's name
@return the name of the planet
*/
public String getName()
{
return name;
}
/** Accessor function for the Planet's weight ratio
@return the ratio of the planet's gravity to that of earth
*/
public double getWeightRatio()
{
return gravity;
}
/** Accessor function for the Planet's year length
@return the number of earth days in the planet's year
*/
public int getDaysInYear()
{
return yearLength;
}
/** Overrides the Object toString method to provide information about the
Planet in String form.
@return a string representing the Planet
*/
public String toString()
{
return name + " gravity = " + gravity + " year length = " + yearLength + " days ";
}
}
###########Spacetravel.java ##########3
package abc;
import abc.Planet;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class SpaceTravel
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
Planet venus=new Planet("Venus", 0.91, 225);
Planet mars=new Planet("Mars", 0.38, 687);
Planet jupiter=new Planet("jupiter", 2.64, 4380);
Planet uranus=new Planet("Uranus", 1.07, 30665);
Planet earth=new Planet("Earth", 1.0, 365);
System.out.println("Please Enter the Space traveller name");
String name=in.nextLine();
System.out.println("Please Enter the Space traveller's Birth date like '28 02 1995' ");
String bdate=in.nextLine();
System.out.println("Please Enter the spcae traveller's weight ");
double weight=in.nextDouble();
int bday=Integer.parseInt(bdate.split(" ")[0].replaceAll("-", "").replaceAll("/", ""));
int bmonth=Integer.parseInt(bdate.split(" ")[1].replaceAll("-", "").replaceAll("/", ""));
int byear=Integer.parseInt(bdate.split(" ")[2].replaceAll("-", "").replaceAll("/", ""));
GregorianCalendar g1=new GregorianCalendar(byear,bmonth,bday);
Date birthdate=g1.getTime();
System.out.println("----------------------------------------------------------------------");
System.out.println("Traveler name: "+name);
System.out.println("Traveler weight: "+weight);
System.out.println("Traveler birth month: "+bmonth);
System.out.println("Traveler birth day: "+bday);
System.out.println("Traveler birth year: "+byear);
System.out.println("======================================================================");
System.out.println("Traveler: "+name);
System.out.println("Weight: "+weight);
System.out.println("Birthday: "+bmonth+"/"+bday+"/"+byear);
System.out.println("----------------------------------------------------------------------");
System.out.println("Name Weight(lbs) Age(Years)");
System.out.println("-----------------------------------------------------------------------");
earth.display(weight, birthdate);
venus.display(weight, birthdate);
mars.display(weight, birthdate);
jupiter.display(weight, birthdate);
uranus.display(weight, birthdate);
System.out.println("========================================================================");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.