-4

I am new to java and I was given 3 files but I do not know how to compile them to get them to work together.

Here are the files:

GetBurnRate.java

import java.io.*;

public class GetBurnRate{
  public static void main(String[] args){

    //Send welcome message
    System.out.println("#Welcome to Lunar Lander");

    try{
      //Begin reading from System input
      BufferedReader inputReader =
      new BufferedReader(new InputStreamReader(System.in));
      //Set initial burn rate to 0
      int burnRate = 0;
      do{
        //Prompt user
        System.out.println("#Enter burn rate or <0 to quit:");
        //Read user response
        try{
          String burnRateString = inputReader.readLine();
          burnRate = Integer.parseInt(burnRateString);
          //Send user-supplied burn rate to next filter
          System.out.println("%" + burnRate);
        } catch(NumberFormatException nfe){
          System.out.println("#Invalid burn rate.");
        }
      }
      while(burnRate >= 0);
      inputReader.close();
    } catch(IOException ioe){
      ioe.printStackTrace();
    }
  }
}

CalcNewValues.java

    import java.io.*;

    public class CalcNewValues{

      public static void main(String[] args){
        //Initialize values
        final int GRAVITY = 2;
        int altitude = 1000;
        int fuel = 500;
        int velocity = 70;
        int time = 0;

        try{
          BufferedReader inputReader = new
          BufferedReader(new InputStreamReader(System.in));

          //Print initial values
          System.out.println("%a" + altitude);
          System.out.println("%f" + fuel);
          System.out.println("%v" + velocity);
          System.out.println("%t" + time);

          String inputLine = null;
          do{
            inputLine = inputReader.readLine();
            if((inputLine != null) &&
               (inputLine.length() > 0)){

              if(inputLine.startsWith("#")){
                //This is a status line of text, and
                //should be passed down the pipeline
                System.out.println(inputLine);
              }
              else if(inputLine.startsWith("%")){
                //This is an input burn rate
                try{
                  int burnRate =
                    Integer.parseInt(inputLine.substring(1));
                  if(altitude <= 0){
                    System.out.println("#The game is over.");
                  }
                  else if(burnRate > fuel){
                    System.out.println("#Sorry, you don't" +
                                       "have that much fuel.");
                  }
                  else{
                    //Calculate new application state
                    time = time + 1;
                    altitude = altitude - velocity;
                    velocity = ((velocity + GRAVITY) * 10 -
                                burnRate * 2) / 10;
                    fuel = fuel - burnRate;
                    if(altitude <= 0){
                      altitude = 0;
                      if(velocity <= 5){
                        System.out.println("#You have" +
                                           "landed safely.");
                      }
                      else{
                        System.out.println("#You have" +
                                           "crashed.");
                      }
                    }
                  }
                  //Print new values
                  System.out.println("%a" + altitude);
                  System.out.println("%f" + fuel);
                  System.out.println("%v" + velocity);
                  System.out.println("%t" + time);
                }
                catch(NumberFormatException nfe){
                }
              }
            }
          }
          while((inputLine != null) && (altitude > 0));
          inputReader.close();
        }
        catch(IOException ioe){
          ioe.printStackTrace();
        }
      }
    }

DisplayValues.java

import java.io.*;
public class DisplayValues{
  public static void main(String[] args){
    try{
      BufferedReader inputReader = new
      BufferedReader(new InputStreamReader(System.in));
      String inputLine = null;
      do{
        inputLine = inputReader.readLine();
        if((inputLine != null) &&
           (inputLine.length() > 0)){
          if(inputLine.startsWith("#")){


  //This is a status line of text, and
        //should be passed down the pipeline with
        //the pound-sign stripped off
        System.out.println(inputLine.substring(1));
      }
      else if(inputLine.startsWith("%")){
        //This is a value to display
        if(inputLine.length() > 1){
          try{
            char valueType = inputLine.charAt(1);
            int value =
            Integer.parseInt(inputLine.substring(2));
            switch(valueType){
              case 'a':
                System.out.println("Altitude: " +
                                   value);
                break;
              case 'f':
                System.out.println("Fuel remaining: " +
                                   value);
                break;
              case 'v':
                System.out.println("Current Velocity: "
                                   + value);
                break;
              case 't':
                System.out.println("Time elapsed: " +
                                   value);
                break;
            }
          }
          catch(NumberFormatException nfe){
          }
        }
      }
    }
  }
  while(inputLine != null);
  inputReader.close();
}
catch(IOException ioe){
  ioe.printStackTrace();
}
  }
}

Then I am supposed to change GetCalcNewValues.java and translate it to C++ and get it to run as so:

|GetBurnRate.java | GetCalcNewValues.cpp | DisplayValues.java

I only need to know how to properly compiler these, I can do the translation.

  • 1
    [How do I run a Java program from the command line on Windows?](https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows) or any other from the million links on web. Similarly to C++, Java code has to be compiled first, then unsimilarly you have to use `java` program to run it. Note that both your files have `public static void main()` function, so they are to be compiled into two separate programs. – Yksisarvinen Apr 21 '20 at 21:56
  • 2
    I doubt anyone will translate the code from java to c++ for you for free. That is not what StackOverflow is about. – drescherjm Apr 21 '20 at 22:01

1 Answers1

0

To compile a java program with main file GetBurnRate.java type the following into a console (You will need a java compiler installed)

javac GetBurnRate.java

To compile a c++ program you will need a c++ compile installed such as gcc or clang.

For gcc type the following into a console

g++ GetCalcNewValues.cpp -o GetCalcNewValues.exe

For clang

clang++ GetCalcNewValues.cpp -o GetCalcNewValues.exe

If you aren't using windows the file extension for the compiled c++ program probably won't be exe.

To run the programs giving the output from the first as the input to the next use

java GetBurnRate | GetCalcNewValues | java DisplayValues
Isaac Clancy
  • 409
  • 2
  • 7