Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

19.21 Ch 9 Program: Data visualization (Java) (1) Prompt the user for a title fo

ID: 3803034 • Letter: 1

Question

19.21 Ch 9 Program: Data visualization (Java)

(1) Prompt the user for a title for data. Output the title. (1 pt)

Ex:


(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)

Ex:


(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in an ArrayList of strings. Store the integer components of the data points in a second ArrayList of integers. (4 pts)

Ex:


(4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point.

If entry has no comma

Output: Error: No comma in string. (1 pt)

If entry has more than one comma

Output: Error: Too many commas in input. (1 pt)

If entry after the comma is not an integer

Output: Error: Comma not followed by an integer. (2 pts)


Ex:


(5) Output the information in a formatted table. The title is right justified with a minimum of 33 characters. Column 1 is left justified with a minimum of 20 characters. Column 2 is right justified with a minimum of 23 characters. (3 pts)

Ex:


(6) Output the information as a formatted histogram. Each name is right justified with a minimum of 20 characters. (4 pts)

Ex:

Explanation / Answer

JAVA PROGRAM :


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;


public class Main{
  
    //Object for generating random number
    private static final Random rand = new Random();
  
  
    //Method to calculate GCD for printing ratio
    private static int gcd(int a, int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
  
  
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //Prompting and reading username
        System.out.println("Enter a title for the data:");
        String title = br.readLine().trim();
        System.out.println("You entered: "+title);
      
        System.out.println("Enter the column 1 header:");
        String col1 = br.readLine().trim();
        System.out.println("You entered: "+col1);
      
      
        System.out.println("Enter the column 2 header:");
        String col2 = br.readLine().trim();
        System.out.println("You entered: "+col2);
      
        String str;
      
        ArrayList<String> al = new ArrayList();
        System.out.println("Enter the data point(-1 to stop the input)");
        while(!(str=br.readLine()).equals("-1")){
          
            int c=0;
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)==','){
                    c++;
                }
            }
            if(c==0){
                System.out.println("Error: No comma in string");
                continue;
            }
            if(c>1){
                System.out.println("Error: Too many commas in input.");
                continue;
            }
            String data[] = str.trim().split(",");
            try{
                int x = Integer.parseInt(data[1].trim());
            }
            catch(NumberFormatException e){
                System.out.println("Error: Comma not followed by an integer");
                continue;
            }
            al.add(str);
            System.out.println("Enter the data point(-1 to stop the input)");
        }
        for(String x : al){
            String data[] = x.trim().split(",");
            System.out.println("Data String "+data[0]);
            System.out.println("Data Integer "+Integer.parseInt(data[1].trim()));
        }
      
        System.out.println(title);
        System.out.println(col1+"          |           "+col2);
        for(String x : al){
            String data[] = x.trim().split(",");
            System.out.println(data[0]+"                    "+data[1]);
        }
      
        for(String x : al){
            String data[] = x.trim().split(",");
            int n = Integer.parseInt(data[1].trim());
            System.out.print(data[0]+"                    ");
            for(int i=1;i<=n;i++){
                System.out.print("*");
            }
            System.out.println("");
        }
      
    }
}

OUTPUT :

Enter a title for the data:
Java Program
You entered: Java Program
Enter the column 1 header:
Name
You entered: Name
Enter the column 2 header:
Count
You entered: Count
Enter the data point(-1 to stop the input)
Stephen King , 14
Enter the data point(-1 to stop the input)
Jane Austin , 16
Enter the data point(-1 to stop the input)
Andrea , 4
Enter the data point(-1 to stop the input)
Oscar , 11
Enter the data point(-1 to stop the input)
Charles Dickens, 20
Enter the data point(-1 to stop the input)
-1
Data String Stephen King
Data Integer 14
Data String Jane Austin
Data Integer 16
Data String Andrea
Data Integer 4
Data String Oscar
Data Integer 11
Data String Charles Dickens
Data Integer 20
Java Program
Name          |           Count
Stephen King                      14
Jane Austin                      16
Andrea                      4
Oscar                      11
Charles Dickens                     20
Stephen King                     **************
Jane Austin                     ****************
Andrea                     ****
Oscar                     ***********
Charles Dickens                    ********************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote