Question

You will have to write one method in the GradeHistogram class The method should be called...

You will have to write one method in the GradeHistogram class

  • The method should be called printDataRow
  • It has two formal parameters
    A string for the row label
    An int for how many asterisks are in the row
  • The method will print
    The string that was passed in followed by a colon and a space
    The asterisks (*)
    After the last asterisk it will print a newline
    Example: the statement: printDataRow("Number of As", 7); would output the following Number of As: *******
  • The class GradeHistogram is:
    • public class GradeHistogram
      {
      private String className;
      private int numAs;
      private int numBs;
      private int numCs;
      private int numDs;
      private int numFs;
        
      public GradeHistogram(String className, int numAs, int numBs, int numCs,int numDs, int numFs)
      {
      this.className = className;
      this.numAs = numAs;
      this.numBs = numBs;
      this.numCs = numCs;
      this.numDs = numDs;
      this.numFs = numFs;
      }
        
      public void setClassName(String name)
      {
      className = name;
      }
      public void setNumAs(int num)
      {
      numAs = num;
      }
      public void setNumBs(int num)
      {
      numBs = num;
      }
      public void setNumCs(int num)
      {
      numCs = num;
      }
      public void setNumDs(int num)
      {
      numDs = num;
      }
      public void setNumFs(int num)
      {
      numFs = num;
      }
        
      public String getClassName()
      {
      return className;
      }
      public int getNumAs()
      {
      return numAs;
      }
      public int getNumBs()
      {
      return numBs;
      }
      public int getNumCs()
      {
      return numCs;
      }
      public int getNumDs()
      {
      return numDs;
      }
      public int getNumFs()
      {
      return numFs;
      }
        
      public void printDataRow(String rowLabel, int numAst)
      {
      //answer goes here
      }
        
      public void printChart()
      {
      System.out.println("Class Name: " + className);
      printDataRow("Number of As", numAs);
      printDataRow("Number of Bs", numBs);
      printDataRow("Number of Cs", numCs);
      printDataRow("Number of Ds", numDs);
      printDataRow("NUmber of Fs", numFs);
        
      }
        
      }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Code:

public class GradeHistogram
{
private String className;
private int numAs;
private int numBs;
private int numCs;
private int numDs;
private int numFs;

public GradeHistogram(String className, int numAs, int numBs, int numCs,int numDs, int numFs)
{
this.className = className;
this.numAs = numAs;
this.numBs = numBs;
this.numCs = numCs;
this.numDs = numDs;
this.numFs = numFs;
}

public void setClassName(String name)
{
className = name;
}
public void setNumAs(int num)
{
numAs = num;
}
public void setNumBs(int num)
{
numBs = num;
}
public void setNumCs(int num)
{
numCs = num;
}
public void setNumDs(int num)
{
numDs = num;
}
public void setNumFs(int num)
{
numFs = num;
}

public String getClassName()
{
return className;
}
public int getNumAs()
{
return numAs;
}
public int getNumBs()
{
return numBs;
}
public int getNumCs()
{
return numCs;
}
public int getNumDs()
{
return numDs;
}
public int getNumFs()
{
return numFs;
}

public void printDataRow(String rowLabel, int numAst)
{
   //printing the string first
   System.out.print(rowLabel+": ");
   //simple for loop to print the asterisks
   for(int i=1;i<=numAst;i++){
       System.out.print("*");
   }
   //new line character after all the asterisks
   System.out.println();
}

public void printChart()
{
System.out.println("Class Name: " + className);
printDataRow("Number of As", numAs);
printDataRow("Number of Bs", numBs);
printDataRow("Number of Cs", numCs);
printDataRow("Number of Ds", numDs);
printDataRow("NUmber of Fs", numFs);

}
//main method has been added to test the functions
public static void main(String args[]) {
   //Creating object to call the method printChart()
   GradeHistogram ob = new GradeHistogram("CheggIs<3",1,2,3,4,5);
   ob.printChart();
}
}

Console Output:

Add a comment
Know the answer?
Add Answer to:
You will have to write one method in the GradeHistogram class The method should be called...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • ble and instance variable, and then use clas 4. Please discuss the difference between class variable...

    ble and instance variable, and then use clas 4. Please discuss the difference between class variable and instant variable to complete the following code for the count of number of stud public class Student _//q1: fill this line to create a class variable num. public Student 1/ 22: fill this line to increase num. public static int getNum() return num; public static void main(String[] args) { Student stl=new Student O; Student st2-new Student ; Student st3-new Student ; //q3: fill...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT