Question

Using Java, write a program that uses a class to represents a spaceship. A spaceship has...

Using Java, write a program that uses a class to represents a spaceship. A spaceship has some amount of jets, landing gear, parachutes and astronauts. It must leave Earth’s gravity, so it must use all its jets to propel itself off Earth. The spaceship will spend some time exploring before returning to Earth. When it returns to Earth, it will use its parachute and landing gear to land safely. Make sure that those are extended before landing!

Sample output: Your spaceship has 5 astronauts, 2 jets, stored landing gear and parachute.

The spaceship is taking off!

Your spaceship is exploring...

Your spaceship is heading back to Earth.

Oh no! You’re landing gear and parachute weren’t extended! The expedition ended in failure.

Sample output: Your spaceship has 10 astronauts, 4 jets, stored landing gear and parachute.

The spaceship is taking off!

Your spaceship is exploring...

Your spaceship is heading back to Earth.

The landing gear and parachute have been extended!

Your ship has arrived safely back on Earth.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

import java.util.*;
class Spaceship{
int astronauts,jet;
public String toString(){//overriding the toString() method
return "Your spaceship has "+ astronauts +" astronauts, "+jet +" jets, stored landing gear and parachute.";
}
public void takingOff(){
System.out.println("The spaceship is taking off!");
}
public void exploring(){
System.out.println("Your spaceship is exploring...");
}
public void headingBackToEarth(){
System.out.println("Your spaceship is heading back to Earth.");
}
public void landingGearsExtended(){
System.out.println("The landing gear and parachute have been extended! \n Your ship has arrived safely back on Earth.");
}
public void landingGearsNotExtended(){
System.out.println("Oh no! You’re landing gear and parachute weren’t extended! The expedition ended in failure.");
}
  
}
public class Main
{
   public static void main(String[] args) {
       System.out.println("Enter number of astronauts and jets");
       Scanner sc = new Scanner(System.in);
       int astronauts = sc.nextInt();
       int jet = sc.nextInt();
       Spaceship ss = new Spaceship();
       ss.astronauts = astronauts;
       ss.jet = jet;
       System.out.println(ss.toString());
       ss.takingOff();
   ss.exploring();
       ss.headingBackToEarth();
       Random random = new Random();
       boolean res = random.nextBoolean();
       if(res == true)
       ss.landingGearsExtended();
       else
       ss.landingGearsNotExtended();
      
      
   }
}

Screenshots -

Add a comment
Know the answer?
Add Answer to:
Using Java, write a program that uses a class to represents a spaceship. A spaceship has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python, write a program that uses a class to represents a spaceship. A spaceship has...

    In Python, write a program that uses a class to represents a spaceship. A spaceship has some amount of jets, landing gear, parachutes and astronauts. It must leave Earth’s gravity, so it must use all its jets to propel itself off Earth. The spaceship will spend some time exploring before returning to Earth. When it returns to Earth, it will use its parachute and landing gear to land safely. Make sure that those are extended before landing! Sample output:        Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT