Task: Define Bar Chart Class in Java Program
Define Bar Chart Class in Java Program-Specification
Define Sales Bar Chart Class in Java Program
Write a Java program to accomplish the following tasks
1. Define a class BarChart in Java.
2. Add an array of double in it as a field
3. Define two constructors: no arg constructor to initialize with 0.0
and parameterized constructor to initialize with given array containing daily sales of 7 shops.
5. Define a method to print a bar chart of “==”
two equal sign characters, according to 7 sales of shops 6. Define a main class to use BarChart class in Java
The Source Code:Define Bar Chart Class in Java
/* Write a Java program to acomplish the following tasks 1. Define a class BarChart in Java. 2. Add an array of double in it 3. Define two constructors: no arg constructor to initialize with 0.0 and parameterized constructor to initialize with given array containing daily sales of 7 shops. 4. Define a method to show the sales 5. Define a method to print a bar chart of "==" two equal sign characters, according to 7 sales of shops 6. Define a main class to use BarChart class in Java */ package salesbarchart; import java.util.Arrays; /** * @author www.EasyCodebook.com */ class BarChart{ private double sales[]= new double[7]; BarChart(){ Arrays.fill(sales,0.0); } BarChart(double[] s){ sales=s; } public void printBarChart(){ int i,j,hyphen; System.out.println("Bar Chart of Sales on "+java.time.LocalDate.now()); for(i=0;i<7;i++) { hyphen=(int)sales[i] / 100; System.out.print("Sales:Shop#:"+(i+1)+": "); for(j=1;j<=hyphen;j++) System.out.print("=="); System.out.println(" "+sales[i]); } } } public class SalesBarChart { public static void main(String[] args) { double arr[]={800.0,400.0,1500.0,100.0,1100.0,200.0,600.0}; BarChart chart1=new BarChart(arr); chart1.printBarChart(); } }
Output of a sample run of the Java Program for Creating Bar Chart class
Bar Chart of Sales on 2019-07-10 Sales:Shop#:1: ================ 800.0 Sales:Shop#:2: ======== 400.0 Sales:Shop#:3: ============================== 1500.0 Sales:Shop#:4: == 100.0 Sales:Shop#:5: ====================== 1100.0 Sales:Shop#:6: ==== 200.0 Sales:Shop#:7: ============ 600.0