Chase
Chase

Reputation: 5

Are there any changes I should make to the formatting of my code?

My teacher is telling me that I need to work on my formatting by using code blocks properly. I've edited the code and would like to know if anyone has any ideas for improvement, or if my code looks ok. I'm just not sure if I am formatting it correctly. I didn't lose many points on the assignment for it, but if there is room for improvement I would greatly appreciate it.

import java.util.Scanner;
public class BarChart{ 
public static void main(String[] args){
    System.out.println("Riley Hall - Assignment 3\n");
    Scanner input = new Scanner(System.in);

    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;
    int n5 = 0;
    int i = 1;

    System.out.println("Enter a number between 1 and 30 ");
    n1 = input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n2= input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n3= input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n4= input.nextInt();

    System.out.println("Enter a number between 1 and 30 ");
    n5= input.nextInt();

    for(i = 1; i <= n1; i++){
        System.out.print("*");
    }   
    System.out.println();//new line

    for(i = 1; i <= n2; i++){
        System.out.print("*");
    }   
    System.out.println();//new line

    for(i = 1; i <= n3; i++){
        System.out.print("*");
    }   
    System.out.println();

    for(i = 1; i <= n4; i++){
        System.out.print("*");
    }   
    System.out.println();

    for(i = 1; i <= n5; i++){
        System.out.print("*");}
    }
    System.out.println();

    input.close();
}

}

This is what the program looked like before I edited it.

import java.util.Scanner;
public class BarChart 
{

public static void main(String[] args) 
{
        /* Instructor Comments:  You need to work on the formatting.  Make sure your 
            code lines up properly.  You have most of your code indented too far. 
            Within the Main method it should be indented one tab stop and 
            all the code lined up.  It should only be indented again if 
            it is inside another code structure such as an if statement 
            or for loop.  */

System.out.println("Riley Hall - Assignment 3\n");
    Scanner input = new Scanner(System.in);
    //initializing variables
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;
    int n5 = 0;
    int i = 1;//index 

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
             /* Instructor Comments: This next line should line up with the rest 
                of the code and not be indented.  */
        n1 = input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n2= input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n3= input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n4= input.nextInt();//store user input

     System.out.println("Enter a number between 1 and 30 ");//prompt user input
        n5= input.nextInt();//store user input

            /* Instructor Comments: Format your for loops properly.  The code blocks 
               should be on there own line.  Not on the same line as the code inside it.
               I am fixing the first one to show you.  */
    for(i = 1; i <= n1; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {
                System.out.print("*");
            }//prints corresponding amount of *'s based on how many times the loop incremented 

            System.out.println();//new line

    for(i = 1; i <= n2; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

    for(i = 1; i <= n3; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

    for(i = 1; i <= n4; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

    for(i = 1; i <= n5; i++)//starts loop at one, loop counts up to the integer the user incremented, increments up to users input one at a time
    {System.out.print("*");}//prints corresponding amount of *'s based on how many times the loop incremented
     System.out.println();//new line

     input.close();//closes scanner
}

}

Upvotes: 0

Views: 33

Answers (2)

Albert
Albert

Reputation: 195

They probably meant for you to use loops when you need to repeat certain patterns. Your main function can be rewritten like this:

  public static void main(String[] args) {
    System.out.println("Riley Hall - Assignment 3\n");
    Scanner input = new Scanner(System.in);

    int[] n = new int[5];
    int i, j;

    for (i = 0; i < 5; i++) {
      System.out.println("Enter a number between 1 and 30 ");
      n[i] = input.nextInt();
    }

    for (i = 0; i < 5; i++) {
      for (j = 0; j < n[i]; j++) {
        System.out.print("*");
      }
      System.out.println();
    }

    input.close();
  }

As you can see, I used loop variables starting from zero index and ending before the array size is reached. This is generally considered good practice, because arrays, for example, start with index 0. If you really want to impress your teacher, you can use methods as well:

  private static int[] readNumbers(int amount) {
    Scanner input = new Scanner(System.in);

    int[] n = new int[amount];
    int i;

    for (i = 0; i < 5; i++) {
      System.out.println("Enter a number between 1 and 30 ");
      n[i] = input.nextInt();
    }

    input.close();

    return n;
  }

  private static void printStars(int[] amounts) {
    int i, j;

    for (i = 0; i < amounts.length; i++) {
      for (j = 0; j < amounts[i]; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    System.out.println("Riley Hall - Assignment 3\n");

    int[] n = readNumbers(5);
    printStars(n);
  }

They make code reusable and are infinitely useful.

As per formatting, you can always get IntelliJ IDEA community edition and use its auto format feature to see how a generally good formatting looks. You shouldn't rely on this though to do the job for you until you're comfortable with keeping the format yourself.

Upvotes: 0

Aaron
Aaron

Reputation: 353

One thing to note about formatting code is that most of the time, the format of code does not impact how it runs. (In some cases you could write most of your code on one line and it would still work).

Formatting is one of those things that is designed to make it easier on the developer to debug and change or add code. With this in mind, everyone can format their code in whatever way they find it suits them. You shouldn't have lost marks for a piece of code that does work.

Here are some well-known practices of formatting code:

Hierarchical

Think of this as a hierarchy. If you place an object within a class, or a function within a function, you format it to represent that. For example:

import java.util.Scanner;
public class BarChart{ 
public static void main(String[] args){

Would become:

import java.util.Scanner;
public class BarChart{ 
   public static void main(String[] args){

See how this represents that public static void main(String[] args){ is within public class BarChart{? It makes it a lot easier to see it visually. Use your parentheses to represent a closure of a function, and indent it at the same level as the declaration. Of course, this brings up the never-ending debate about whether you use tab or space to indent your code, but really - it doesn't matter. It's your choice.

Spacing

I once read somewhere that if you believe a comment would be most suitable in between two lines of code, leave a gap. This especially applies between two different functions or the like (which it appears you've already done).

Commenting

Ah, commenting. It's saved me from hours of trying to remember why I wrote something like that, or why something wasn't working. Always try to comment your code. It's the one time you can convert all the computer language into human-readable language and it will help you better understand your code. If you need to pass the code on, the new developer will be able to understand it better. If you need to fix something a year later, you will be able to recall how it worked and possibly what needs to be changed. Comment, comment, comment.

These are just some of my favourite practices and some you should adapt too. These are by no means professional practices and they're just the basics of coding. To answer your original question, your code is very well structured but try to focus on indenting and commenting.

(Note: if you were talking about the structure of your code and not the format, you should look into not repeating functions. This makes your code look bulky).

Upvotes: 1

Related Questions