converting from if-else to methods

Viewed 801

Here is my current code:

import java.util.Scanner;//importing scanner 

public class QuestionOne {

    public static void main(String[] args) {
        int numberofDays;//these two lines define variables
        int sharePoints;

        Scanner keyboard = new Scanner(System.in);//activating scanner
        System.out.print("Number of days in the period: ");//asking question

        numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

        System.out.print("Share points on the first day: ");//asking another question
        sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

        while (numberofDays < 10 || numberofDays > 20)//while loop makes sure the conditions stay true
        {
            System.out.println("The number of days doesn’t meet the required criteria, enter it again");
            System.out.print("Number of days in the period: ");
            numberofDays = keyboard.nextInt();

            //above three lines ask for number of days until a value that fits within specification is obtained
        }

        System.out.println("Day " + " Share Points");
        System.out.println(1 + "    " + sharePoints);

        //above two lines print day and share points, as well as the first line of text (as it does not change)

        for (int i = 2; i <= numberofDays; i++) {
            if (numberofDays % 2 == 0)

                if (i <= numberofDays / 2) {
                    sharePoints = sharePoints + 50;
                    System.out.println(i + "    " + sharePoints);
                } else {
                    sharePoints = sharePoints - 25;
                    System.out.println(i + "    " + sharePoints);
                } else {
                    if (i <= numberofDays / 2 + 1) {
                        sharePoints = sharePoints + 50;
                        System.out.println(i + "    " + sharePoints);
                } else {
                    sharePoints = sharePoints - 25;
                    System.out.println(i + "    " + sharePoints);

                    // above nested if else statements essentially calculate and concatenate the variables to obtain an answer that is then printed and repeated until the number of days is reached (starting from day two)
                }
            }
        }
    }
}

This code compiles and works as I want, however, I no longer want it to be in this format. Instead, I would like for it to contain a method named DisplayStock; the input arguments I want for this method are the number of days in the period and the share points on the first day. The method is used to increase the share points by 50 and decrease the share points by 25 on alternate days in the specified period. The method then displays a table showing the days and the share points on those days. This method doesn’t return anything.

As for the main method, it will first ask the users to enter the number of days in the specified period and the share points on the first day (with input validation, the program should then call the DisplayStock method that outputs the table.

A sample output currently looks as such if the period is ll days and SharePoint are 550:

Day Share Points
1 550
2 600
3 575
4 625
5 600
6 650
7 625
8 675
9 650
10 700
11 675

So basically, what I am intending to do is convert the code from if-else statements over to methods to alleviate issues with readability and function. Any help would be appreciated! I will continue to work on this but I do not think I will be able to get as far as I intend.

3 Answers

you just want to break the code in to two methods or several basic will be this:

  public static void main(String[] args) {
    int numberofDays;//these two lines define variables
    int sharePoints;

    Scanner keyboard = new Scanner(System.in);//activating scanner
    System.out.print("Number of days in the period: ");//asking question

    numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

    System.out.print("Share points on the first day: ");//asking another question
    sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

    numberofDays = validator(numberofDays,keyboard); // Validates Keyboard input

    //above two lines print day and share points, as well as the first line of text (as it does not change)
    outPutTablePrinter(numberofDays,sharePoints);

}

private static void outPutTablePrinter(int numberOfDays,int sharePoints){
    System.out.println("Day " + " Share Points");
    System.out.println(1 + "    " + sharePoints);
    for (int i = 2; i <= numberOfDays; i++) {
        if (numberOfDays % 2 == 0)
            if (i <= numberOfDays / 2) {
                sharePoints = sharePoints + 50;
                System.out.println(i + "    " + sharePoints);
            } else {
                sharePoints = sharePoints - 25;
                System.out.println(i + "    " + sharePoints);
            } else {
            if (i <= numberOfDays / 2 + 1) {
                sharePoints = sharePoints + 50;
                System.out.println(i + "    " + sharePoints);
            } else {
                sharePoints = sharePoints - 25;
                System.out.println(i + "    " + sharePoints);
                // above nested if else statements essentially calculate and concatenate the variables to obtain an answer that is then printed and repeated until the number of days is reached (starting from day two)
            }
        }
    }
}

private static int validator(int numberOfDays,Scanner keyboard){
    while (numberOfDays < 10 || numberOfDays > 20)//while loop makes sure the conditions stay true
    {
        System.out.println("The number of days doesn’t meet the required criteria, enter it again");
        System.out.print("Number of days in the period: ");
        numberOfDays = keyboard.nextInt();
    }
    return numberOfDays;
}

I refactored your code. First, I would prefer printf and your logic can be greatly simplified by incrementing by two in your loop. You do need to check if there is an index + 1 for it to work correctly with odd number of days, but you can do

System.out.printf("Day\tShare Points%n");
for (int i = 0; i < numberofDays; i += 2) {
    System.out.printf("%-3d\t%d%n", i + 1, sharePoints);
    sharePoints += 50;
    if (i + 1 < numberofDays) {
        System.out.printf("%-3d\t%d%n", i + 2, sharePoints);
        sharePoints -= 25;
    }
}

And I get

Day Share Points
1   550
2   600
3   575
4   625
5   600
6   650
7   625
8   675
9   650
10  700
11  675

Following code be used:

import java.util.Scanner;//importing scanner 

public class QuestionOne{
      static int numberofDays;//these two lines define variables
      static int sharePoints;

  public static void main(String[]args){


      Scanner keyboard = new Scanner (System.in);//activating scanner

      System.out.print("Number of days in the period: ");//asking question

      numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

      System.out.print("Share points on the first day: ");//asking another question

      sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input

    while (numberofDays < 10 || numberofDays > 20)//while loop makes sure the conditions stay true
     {
        System.out.println("The number of days doesn’t meet the required criteria, enter it again");

        System.out.print("Number of days in the period: ");

        numberofDays = keyboard.nextInt();

        //above three lines ask for number of days until a value that fits within specification is obtained
      } 

    DisplayStock ();

  }
  public static void DisplayStock (){
      System.out.println("Day " + " Share Points");
      System.out.println(1 + "    " + sharePoints);

      //above two lines print day and share points, as well as the first line of text (as it does not change)

      for(int i = 2; i <= numberofDays; i++)
      {
        if(numberofDays % 2 == 0)

          if(i <= numberofDays/2)
          {
          sharePoints = sharePoints + 50;  
          System.out.println(i + "    " + sharePoints);
        }
        else
        {
          sharePoints = sharePoints - 25;
          System.out.println(i + "    " + sharePoints);
        }
        else
        {
          if(i <= numberofDays/2 + 1)
          {
            sharePoints = sharePoints + 50;
            System.out.println(i + "    " + sharePoints);
          }
          else
          {
            sharePoints = sharePoints - 25;
            System.out.println(i + "    " + sharePoints);

            //above nested if else statements essentially calculate and concatenate the variables to obtain an answer that is then printed and repeated until the number of days is reached (starting from day two)
            }
        }
      }
  }
}
Related