Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am stuck at this OOP question. Please refer to Question 3 (Bar Chart Racer) at https: // coursera.cs.princeton.edu/introcs/assignments/barchart/specification.php . I think question 3 is

I am stuck at this OOP question. Please refer to Question 3 (Bar Chart Racer) at https://coursera.cs.princeton.edu/introcs/assignments/barchart/specification.php . I think question 3 is related to both question 1 and 2. The code for question 2 is provided and can be accessed within the link. My answer for Q1 is:

import java.util.Arrays; public class Bar implements Comparable<Bar> { private final String n; private final int v; private final String c; public Bar(String name, int value, String category) { n = name; v = value; c = category; } public String getName() { if (n == null) throw new IllegalArgumentException("invalid name: " + null); return n; } public int getValue() { if (v <= 0) throw new IllegalArgumentException("invalid value: " + v); return v; } public String getCategory() { if (c == null) throw new IllegalArgumentException("invalid category: " + null); return c; } public String toString() { return getName() + ", " + getValue() + ", " + getCategory(); } public int compareTo(Bar that) { if (that == null) throw new NullPointerException(); return Integer.compare(this.v, that.v); } public static void main(String[] args) { Bar[] bars = new Bar[10]; bars[0] = new Bar("Beijing", 22674, "East Asia"); bars[1] = new Bar("Cairo", 19850, "Middle East"); bars[2] = new Bar("Delhi", 27890, "South Asia"); bars[3] = new Bar("Dhaka", 19633, "South Asia"); bars[4] = new Bar("Mexico City", 21520, "Latin America"); bars[5] = new Bar("Mumbai", 22120, "South Asia"); bars[6] = new Bar("Osaka", 20409, "East Asia"); bars[7] = new Bar("So Paulo", 21698, "Latin America"); bars[8] = new Bar("Shanghai", 25779, "East Asia"); bars[9] = new Bar("Tokyo", 38194, "East Asia"); Arrays.sort(bars); for (int i = 0; i < bars.length; i++) StdOut.println(bars[i]); } } 

For Q3, I need to have a public BarChartRacer() constructor but i'm not sure what's it's suppose to do? My main method so far is:

public static void main(String[] args) { // create the bar chart String fileName = args[0]; int k = Integer.parseInt(args[1]); //no of bars to display In file = new In(fileName); String title = file.readString(); String xAxis = file.readString(); String source = file.readString(); BarChart chart = new BarChart(title, xAxis, source); String year = "", name = "", country = "", category = ""; int value = 0; while (file.hasNextLine()) { int n = Integer.parseInt(file.readLine()); //no of records in group Bar[] bars = new Bar[n]; //nth no of bars for (int i = 0; i < n; i++) { String record = file.readString(); //read each line of record String[] separateRec = record.split(","); //separate each item within line separateRec[0] = year; separateRec[1] = name; separateRec[2] = country; separateRec[3] = String.valueOf(value); separateRec[4] = category; bars[i] = new Bar(name, value, category); } Arrays.sort(bars); //this sort bars in ascending order //bars in descending order Bar[] bars1 = new Bar[n]; int j = n; for (int x = 0; x < n; x++) { bars1[j-1] = bars[x]; j = j - 1; } String[] nam = new String[n]; int[] val = new int[n]; String[] cat = new String[n]; for (int i = 0; i < n; i++) { nam[i] = name; val[i] = value; cat[i] = category; chart.add(nam[i], val[i], cat[i]); } for (int d = 0; d < k; d++) { // draw the bar chart StdDraw.setCanvasSize(1000, 700); StdDraw.enableDoubleBuffering(); StdDraw.clear(); chart.draw(); StdDraw.show(); StdDraw.pause(100); } chart.setCaption(year); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions