Back again with another school lab - this time in Java!
The problem we're meant to solve is this:
The Huntington High School basketball team has five players named Art, Bob, Cal, Dan, and Eli. Accept the number of points scored by each player in a game and create a bar chart that illustrates the points scored by displaying an asterisk for each point.
The end result is meant to look something like this:
Art *****
Bob ********
Cal ***
Dan *******
Eli ************
This problem comes up in the Looping chapter of the book, and unfortunately, arrays have not yet been introduced. I know it would be faster and more concise to use a few arrays to handle this problem, but I'm going to just rely on things that we've seen in the book already.
So lets break the problem down:
We need to get point totals for each of the players.
This means we need to get user input, so we know we'll need the Scanner utility.
We'll also need to print several lines asking for the totals from the user.
These totals need to be assigned to a variable for each player.
We need to print lines to display the totals.
For this we'll print a line indicating the players name.
We'll then create a loop which loops through each players points and adds an asterisk to the printed line.
With the problem broken down into smaller pieces, it doesn't seem so difficult! Lets get into it:
Lets initialize the Java environment and import the scanner utility:
import java.util.Scanner();
public class BarChart {
public static void main(String[] args){
Scanner inputDevice = new Scanner(System.in);
}
}
From here, we'll start asking for user input to assign point values to variables for each player.
import java.util.Scanner();
public class BarChart {
public static void main(String[] args){
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter points earned by Art >> ");
int artPoints = inputDevice.nextInt();
// We'll repeat this for each of the remaining players.
}
}
After compiling and running this, the user is prompted to add an integer for each player, which is then stored in their associated variable (artPoints, bobPoints, calPoints, etc.).
From here, we start displaying the totals.
import java.util.Scanner;
public class BarChart {
public static void main(String[] args) {
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter points earned by Art >> ");
int artPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Bob >> ");
int bobPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Cal >> ");
int calPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Dan >> ");
int danPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Eli >> ");
int eliPoints = inputDevice.nextInt();
System.out.println();
System.out.println("Points for Game");
System.out.println();
System.out.print("Art ");
for(int i = 0; i < artPoints; ++i) {
System.out.print("*");
}
// Again, we'll repeat this for each player.
}
}
The loop works in the following way:
We initalize and set the variable "i" to 0. This is then compared against the players point variable, and incremented by 1.
As long as "i" is less than the players points, it will print an asterisk. Once "i" is equal to the players points it stops printing and exits the loop.
We repeat this for each of the players and we end up with the following:
import java.util.Scanner;
public class BarChart {
public static void main(String[] args) {
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter points earned by Art >> ");
int artPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Bob >> ");
int bobPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Cal >> ");
int calPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Dan >> ");
int danPoints = inputDevice.nextInt();
System.out.print("Enter points earned by Eli >> ");
int eliPoints = inputDevice.nextInt();
System.out.println();
System.out.println("Points for Game");
System.out.println();
System.out.print("Art ");
for(int i = 0; i < artPoints; ++i) {
System.out.print("*");
}
System.out.println();
System.out.print("Bob ");
for(int i = 0; i < bobPoints; ++i) {
System.out.print("*");
}
System.out.println();
System.out.print("Cal ");
for(int i = 0; i < calPoints; ++i) {
System.out.print("*");
}
System.out.println();
System.out.print("Dan ");
for(int i = 0; i < danPoints; ++i) {
System.out.print("*");
}
System.out.println();
System.out.print("Eli ");
for(int i = 0; i < eliPoints; ++i) {
System.out.print("*");
}
}
}
When we compile and run this, and feed the values 5, 10, 15, 10, and 5 into the points prompt - we get the following output:
That's all there is to it! Again, if we were able to use arrays in this lab this could be compacted down but still maintain the same function. I have a feeling that later on in the book we'll be revisiting this problem to do just that.
Until next time, take care, and as always feel free to leave comments below with tips and tricks!