How can I make the histogram more OOPy without importing any Java library?


Dominic Alexander

I'm making a histogram project for school that will loop through an array and create a * for each given number part of a number. The output should look like this:

1-10   | *****
11-20  | ****
21-30  | *
31-40  | ***
41-50  | *
51-60  | *
61-70  | *
71-80  | 
81-90  | *
91-100 | ********

For the following arrays:

int array [] = {19、4、15、7、11、9、13、5、45、56、1、67、90、98、32、36、33、25、99、95、97、98, 92、94、93、105};

I'm trying to figure out a way to make this code more OOPy. However, I can't find a way.

Here is my code:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int array[] = {19, 4, 15, 7, 11, 9, 13, 5, 45, 56, 1, 67, 90, 98, 32, 36, 33, 25, 99, 95, 97, 98, 92, 94, 93, 105};
        histogram(array);
    } 
    public static void histogram(int[] input) {
        String output1 = new String ();
        String output2 = new String ();
        String output3 = new String ();
        String output4 = new String ();
        String output5 = new String ();
        String output6 = new String ();
        String output7 = new String ();
        String output8 = new String ();
        String output9 = new String ();
        String output10 = new String ();
        for (int x = 0; x < input.length; x++) {
            if (input[x] <= 100) {
                if (input[x] >= 1 && input [x] <= 10) output1 += "*";
                if (input[x] >= 11 && input [x] <= 20) output2 += "*";
                if (input[x] >= 21 && input [x] <= 30) output3 += "*";
                if (input[x] >= 31 && input [x] <= 40) output4 += "*";
                if (input[x] >= 41 && input [x] <= 50) output5 += "*";
                if (input[x] >= 51 && input [x] <= 60) output6 += "*";
                if (input[x] >= 61 && input [x] <= 70) output7 += "*";
                if (input[x] >= 71 && input [x] <= 80) output8 += "*";
                if (input[x] >= 81 && input [x] <= 90) output9 += "*";
                if (input[x] >= 91 && input [x] <= 100) output10 += "*";
            }
            else {
                break;
            }
        }
        System.out.println("1-10   | " + output1);
        System.out.println("11-20  | " + output2);
        System.out.println("21-30  | " + output3);
        System.out.println("31-40  | " + output4);
        System.out.println("41-50  | " + output5);
        System.out.println("51-60  | " + output6);
        System.out.println("61-70  | " + output7);
        System.out.println("71-80  | " + output8);
        System.out.println("81-90  | " + output9);
        System.out.println("91-100 | " + output10);

    }
}
Eritreans

Why make it simple when you can make it complicated?

public static void main(String[] args) {         
   int array[] = {19, 4, 15, 7, 11, 9, 13, 5, 45, 56, 1, 67, 90, 98, 32, 36, 33, 25, 99, 95, 97, 98, 92, 94, 93, 105}; 
   histogram(array);
}
public static void histogram(int[] input) {
    for(int i = 1; i<101; i = i+10){
        System.out.print(i + "-" + (i+9) + "\t| ");
        for(int j = 0; j< input.length; j++){
            if(input[j] >= i && input[j] < i+10)
            System.out.print("*");
        }
        System.out.println();
    }
}

Related


How can I make this histogram in ggplot/R?

Crimea Please find My data qbelow . I have two covariates: q$Studieand q$best.respcorresponding to each of the five different studies reporting the best response after a certain treatment. q$best.respThere are three levels table(q$best.resp) 0 1 2 62 42

How can I make this histogram in ggplot/R?

Crimea Please find My data qbelow . I have two covariates: q$Studieand q$best.respcorresponding to each of the five different studies reporting the best response after a certain treatment. q$best.respThere are three levels table(q$best.resp) 0 1 2 62 42

How can I make my own timer without the standard library?

Ansabi Can I use to make a specific number of iterations for loopso that it takes 1 second to fully execute the loop? For example, the following code took 0.125s to execute on my computer: #include <iostream> #include <cmath> using namespace std; int main()

How can I make my own timer without the standard library?

Ansabi Can I use to make a specific number of iterations for loopso that it takes 1 second to fully execute the loop? For example, the following code took 0.125s to execute on my computer: #include <iostream> #include <cmath> using namespace std; int main()

How can I make my own timer without the standard library?

Ansabi Can I use to make a specific number of iterations for loopso that it takes 1 second to fully execute the loop? For example, the following code took 0.125s to execute on my computer: #include <iostream> #include <cmath> using namespace std; int main()

How can I make the contract act without calling any function?

Haim Hadad This is a difficult question to explain, I'll try my best: I wrote a contract renewing the ownership of the real estate house. I have seller (owner), buyer and some other functions. But I want this contract to be a temporary one. Since the seller de

How can I make the contract act without calling any function?

Haim Hadad This is a difficult question to explain, I'll try my best: I wrote a contract renewing the ownership of the real estate house. I have seller (owner), buyer and some other functions. But I want this contract to be a temporary one. Since the seller de

How can I make the contract act without calling any function?

Haim Hadad This is a difficult question to explain, I'll try my best: I wrote a contract renewing the ownership of the real estate house. I have seller (owner), buyer and some other functions. But I want this contract to be a temporary one. Since the seller de

How can I make the contract act without calling any function?

Haim Hadad This is a difficult question to explain, I'll try my best: I wrote a contract renewing the ownership of the real estate house. I have seller (owner), buyer and some other functions. But I want this contract to be a temporary one. Since the seller de

How can I make the contract act without calling any function?

Haim Hadad This is a difficult question to explain, I'll try my best: I wrote a contract renewing the ownership of the real estate house. I have seller (owner), buyer and some other functions. But I want this contract to be a temporary one. Since the seller de

How can I run a module without importing it?

Fuchs I am creating an es6 module that extends RegExp object. I am Object.definePropertiesdoing it using: Object.defineProperties(RegExp.prototoype, { ... }); I don't return anything in this file because RegExp is global in in so I don't need it Node.js, bu

How can I run a module without importing it?

Fuchs I am creating an es6 module that extends RegExp object. I am Object.definePropertiesdoing it using: Object.defineProperties(RegExp.prototoype, { ... }); I don't return anything in this file because RegExp is global in in so I don't need it Node.js, bu

How can I run a module without importing it?

Fuchs I am creating an es6 module that extends RegExp object. I am Object.definePropertiesdoing it using: Object.defineProperties(RegExp.prototoype, { ... }); I don't return anything in this file because RegExp is global in in so I don't need it Node.js, bu

How can I run a module without importing it?

Fuchs I am creating an es6 module that extends RegExp object. I am Object.definePropertiesdoing it using: Object.defineProperties(RegExp.prototoype, { ... }); I don't return anything in this file because RegExp is global in in so I don't need it Node.js, bu