Results 1 to 2 of 2

Thread: new to java

  1. #1
    crise1967 is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default new to java

    I am in a beginning java class and really struggling with this program. Can anyone help please?

    //Write a base class Chart that generates an array of 20 integer values (in its constructor), each of which randomly lies
    //in the range of 1 and 20. The class will also include an abstract plot() method for plotting the data. Define two subclasses,
    //BarChart and LineChart, which implement the plot() function and can plot the data in the array as a vertical bar chart and as a
    //vertical line chart, respectively. To plot a chart, record the chart in a 2-dimension array and print the array.
    //To generate a random number, use "import java.util.Random;" at the very beginning of your program, then create a Random object
    //"Random random = new Random();", and use the "int nextInt(int n)" function in the Random class.
    //You may print the data in the following styles (Suppose the array is 3, 2, 2, and 3. The actual array has 20 values.):

    //* *
    //* * * *
    //* * * *

    //- -
    // - -

    //The line chart only shows the top of the bar, similar to the wave form you see in an mp3 player. Regarding the 2-dimension array for printing purpose,
    //if the array is used for printing the BarChart, the first row of the array in the above example would be {"*", " ", " ", "*"}.

    //Using a menu, user can select to create BarChart or LineChart object. Then, user can select print menu command and print the object using its
    //correct plot method. Your program should demonstrate the run-time polymorphism of Java.

    //For example, your menu can be:
    //1. Create BarChart object
    //2. Create LineChart object
    //3. Print
    //4. Exit

    //When user selects “Print”, the program automatically prints bar or line chart based on the object user has just created.

    I have some code (really bad) but here it is.

    Java Code:
    import java.util.Random;
    private class RandomInterger {
    	Random random = new Random();
    		for (int n = 1; n <= 20; n++)
    		int nextint = new(int n);
    private int n; 
    private int A[][];
    MyArray (int n)	{
    this.n = n;
    A = new int[n][n];
    int array_val=1;
    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){ 
    void print_MyArray(MyArray ob) {
    for(int i=0; i < ob.n; i++) {
    for(int j=0; j < ob.n; j++) {
    public class Assign06 {
      public static void main(String args[])
        throws {
        char choice;
    	Chart c = new Chart();
        for(;;) {
          do {
            System.out.println("Select Chart Type:");
            System.out.println("  1. Bar Chart");
            System.out.println("  2. Line Chart");
            System.out.println("  3. Print Chart\n");
            System.out.print("Choose one (q to quit): ");
            do {
              choice = (char);
            } while(choice == '\n' | choice == '\r');
          } while( choice < '1' | choice > '3' & choice != 'q');
          if(choice == 'q') break;
          switch(choice) {
            case '1':
              c.chart {"*", " ", " ", "*"};
              c.chart {"*", "*", "*", "*"};
              c.chart {"*", "*", "*", "*"};
            case '2':
              c.chart {"-", " ", " ", "-"};
              c.chart {" ", "-", "-", " "};
            case '3':
              c.chart {"*", "*", "*", "*"};
              break; System.out.println("The Bar Chart:\n");
              System.out.println("while(condition) statement;");
            case '4':
              System.out.println("The Line Chart:\n");
              System.out.println("while(condition) statement;");
            case '5':
              System.out.println("The do-while:\n");
              System.out.println("do {");
              System.out.println("  statement;");
              System.out.println("} while (condition);");
            case '6':
              System.out.println("The break:\n");
              System.out.println("break; or break label;");
            case '7':
    public class Assign06 {
    public static void main(String args[]) {
    MyArray my_array=new MyArray(20);
    Last edited by pbrockway2; 03-29-2012 at 10:28 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: new to java

    Hi crise1967, welcome to the forums!

    When you post code use the "code" tags. Put [code] and [/code] at the end: that way the code will be readable when it appears here.

    Formatting (indentation) will help too. Remember that people will first just glance at your post and whether they reply or not will depend somewhat on how readable it is. In the same vein, people are looking for a specific, precise, descriptive *question*.


    From the look of it, that code doesn't compile. If you can't understand a compiler message, post it and someone is sure to explain what it means. (That's what I mean by a specific question.)

    If there are *lots* of compiler messages, consider starting again. The problem is posed in such a way that you can follow it step by step. For what it's worth here's how I would attack the question: First I would read it and make sure I understood it (ie I would know what classes I was going to write and what their methods will be). If in doubt, you can say what you think it's getting at and ask.

    Then I would move through each of the things it says to do beginning with the first couple of sentences "Write a base class Chart that ... include an abstract plot() method." I would compile often. I mean every line of code or so. And address *every* thing about which the compiler complains.

    As each piece of functionality is written, it should be tested. So the first piece of output (as soon as I could write it) would be a simple listing of the array represented by Chart to check that its contents were within range and random looking.


    I guess all I'm saying is work step by step. And ask about the particular step you are working on so that you never end up "struggling" simply because you are lost.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts