G. Yedkois
G. Yedkois

Reputation: 33

Calling other methods to main in java

I am having a little issue with formatting returned methods in the main method. I have created the methods and done the calculation, but my issue is if i am calling the other two methods to the main method correctly. I am also having and issue with formatting each method in columns. Do i need to make the columns in the respected methods? or do i need to create them in the main method?

Write a program that analyzes an object falling for 10 seconds. It should contain main and two additional methods. One of the additional methods should return the distance an object falls in meters when passed the current second as an argument. See the formula needed below. The third method should convert meters to feet. You can look up the conversion factor needed online. The main method should use one loop to call the other methods and generate a table as shown below. The table should be displayed in formatted columns with decimals as shown. I believe i am on

SEC    METERS      FEET
1         4.9      16.1
2        19.6      64.3
3        44.1     144.7
4        78.4     257.2
5       122.5     401.9
6       176.4     578.7
7       240.1     787.7 
8       313.6    1028.9 
9       396.9    1302.2
10      490.0    1607.6

My code

package week4.yedkois;

public class project3 {

    public static void main(String[] args) {
        System.out.printf("SEC" + "\n");

        meters();
        feet();

        for (int time = 1; time <= 10; time++) {
            System.out.println(time);

        }

    }

    public static void meters() {

        double Meters;
        double G = 9.8; // meters = .5(9.8)(seconds) ^2
        for (int time = 1; time <= 10; time++) {
            Meters = (.5 * 9.8 * Math.pow(time, 2));
            System.out.printf("%.1f\n", Meters);
        }

        return;

    }

    public static void feet() {

        double Feet;
        double G = 9.8; // meters = .5(9.8)(seconds) ^2
        for (int time = 1; time <= 10; time++) {
            Feet = (.5 * 9.8 * Math.pow(time, 2) * 3.28084);
            System.out.printf("%.1f\n", Feet);
        }
        return;

    }

}

Upvotes: 0

Views: 919

Answers (1)

user6749601
user6749601

Reputation:

Here is my solution. I use a Tab ("\t") to achieve the same space between the different values. And then I had to redesign your code a little. I use only one if-loop directly in the main-method and hand the current time-value as a parameter into both methods meters() and feet(). That makes it much easier to get all values of one round in line.

Here are some additional remarks:

  • Java is not C++, so you don't have to use an empty return statement at the end of a method. It's useless there.
  • In Java variables and method-names always start with a small letter, _ or $. Only class-names and constants start with a capital letter.

Hope this helps for a start.

public class Project3 {

    public static void main(String[] args){
        System.out.printf("%3s\t%6s\t%6s\n", "SEC", "METERS", "FEET");

        for(int time = 1; time <= 10; time++)
        {
            System.out.print(time + "\t");
            meters(time);
            feet(time);
            System.out.println();
        }
    }

    public static void meters(int time){
        double meters;
        double g = 9.8; // meters = .5(9.8)(seconds) ^2

        meters = (.5 * 9.8 * Math.pow(time, 2));
        // the longer the expected maximum length of a result gets
        // the higher your reserved number of digits has
        // to be, to gain the wanted right bound effect!
        System.out.printf("%6.1f\t", meters);
    }

    public static void feet(int time){
        double feet;
        double g = 9.8; // meters = .5(9.8)(seconds) ^2
        feet = (.5 * 9.8 * Math.pow(time, 2) * 3.28084);
        // the longer the expected maximum length of a result gets
        // the higher your reserved number of digits has
        // to be, to gain the wanted right bound effect!
        System.out.printf("%6.1f", feet);
    }    
}

Upvotes: 1

Related Questions