Help fixing the last few issues in three Java programs please???

Battoe

Member
Jan 11, 2011
43
0
0
These are all due for a class tomorrow, and I've tried everything I know, but I just can't figure out the last few bits. I'm really awful at Java as you'll soon be able to tell. If you guys could figure out a fix for these I would REALLY appreciate it!

The first program is based off of a previous program. Program 9c creates a 10x10 matrix, fills it in with random numbers between 1-100, and then determines the largest number in the matrix. It had to have two methods, one creating the matrix and one finding the largest number. I (stupidly) didn't bring that program home, but I got a 9/10 on it, so I thought I knew what I was doing. 9d was a revision of that program allowing the user to input the number of rows and columns, which he told me to fix. That's here:

Code:
public class Assignment9d
{
    public static void main (String[] args)
    {
        EasyReader console = new EasyReader();
        System.out.print("Input the number of rows you want: ");
        int rows = console.readInt();
        System.out.print("Input the number of columns you want: ");
        int columns = console.readInt();
        int matrix[][]=new int[rows][columns];
        for(int a = 0; a <= (rows-1); ++a)
        {
            for(int b = 0; b <= (columns-1); ++b)
            {
                matrix[b][a] = (int)((Math.random()*100)+1);
            }
        }
        findbig(matrix, rows, columns);
    }
    public static void findbig(int[][] myArray, int bMax, int aMax)
    {
        int rows = bMax;
        int columns = aMax;
        int matrix[][] = myArray;
        int bside = 0;
        int aside = 0;
        for(int a = 0; a <= (rows-1); ++a)
        {
            for(int b = 0; b<= (columns-1); ++b)
            {
                if (matrix[b][a]>matrix[bside][aside])
                {
                    bside = b;
                    aside = a;
                }
            }
        }
        System.out.println("The largest number is " + matrix[bside][aside]);
        System.out.println("The largest number is at (" + bside + " , " + aside+")");
    }
}

Program 9e was supposed to roll 2 die X number of times (entered by the user). It should then count the number of times each number comes up (combine the two rolls). Display the number and percentage of each event. Use an array to keep track of the count (not individual variables). He told me to fix that, it's here:

Code:
public class Assignment9e
{
    public static void main (String[] args)
    {
        EasyReader console = new EasyReader ();
        System.out.println ("Input the number of times you want to roll the die: ");
        int x = console.readInt ();
        int [] myArray = new int [x];
        for (int z=0;z<x;z++)
        {
            int a = (int) (Math.random ()*6) +1;
            int b = (int) (Math.random ()*6) +1;
            int c = a+b;
        }
        for (int e=0;e<myArray.length;e++)
        {
            System.out.println ("You rolled the dice " + " times, or " + "&#37; of the time.");
            System.out.println (" " + (e+2) + " " + myArray[e] + " " + ((double) myArray[e]/x)*100);
        }
    }
}

9f is a revision of that allowing the user to enter the number of sides on the die and the number of die to roll. I got a 14/15 on that... confusing. It's here:

Code:
public class Assignment9f
{
    public static void main (String [ ] args)
    {
        EasyReader console = new EasyReader();
        System.out.print("Input the number of die you want to roll: ");
           int Dice = console.readInt();
           System.out.print("Input the number of sides on the die: ");
           int Sides = console.readInt();
        System.out.print("Input the number of times you want to roll the die: ");
           int times = console.readInt();
           int[] rolls = new int[(Sides*Dice)-(Dice-1)];
           int[] dice = new int[Dice];
           for    (int x=0;x<times;x++)
           {
               int sumDice=0;
            for (int y=0;y<Dice;y++)
            {
                dice[y] =(int)((Math.random()*Sides)+1);
                sumDice += dice[y];
            }
            rolls[sumDice-Dice]++;
           }
           System.out.println("You rolled " + Dice + " dice " + times + " times with " + Sides + " on the dice.");
        for (int y=0;y<rolls.length;y++)
        {
            System.out.println("Number " + "rolls" +  " Percentage");
            System.out.println((y+Dice) + "      " + rolls[y] + "    " +((double)rolls[y]/times)*100 + "%");
        }
    }
}

Finally (and if you got this far you have the patience of a saint), program 11. Write a program to input names from a file. You do not know how many names there are. The program then should give you the option to do one of the following:

a. Print the names (in order)
b. Print the names (reverse order)
c. Search for a name
d. Add to the list
e. Modify a name in the list
f. Delete from the list
g. Exit the program

My attempts at that program are as follows:

Code:
public class Assignment11
{
    public static void main(String[] args)
    {
        EasyReader console = new EasyReader ();
          int x = 10;
          int list [] = new int [x];
          for (int y=0;y<=(x-1);++y)
          {
              list [y] = ((int) (100*Math.random() +1));
              sort (list);
              boolean quit = false;
              while (!quit)
              {
                System.out.println ();
                System.out.println ("Menu");
                System.out.println ("1. Print Array Forward");
                System.out.println ("2. Print Array Backwards");
                System.out.println ("3. Rotate the Array");
                System.out.println ("4. Exit");
                System.out.println ();
                System.out.println ("Enter a number: ");
                int num = console.readInt ();
                console.readLine ();
                switch (num)
                {
                    case 0:
                        quit = true;
                        break;
                      case 1:
                        Print (list, 0);
                        break;
                      case 2:
                        PrintBackwards (list, list.length -1);
                        break;
                      case 3:
                        System.out.println ("Which integer would you like to rotate the array? ");
                        int rotate = console.readInt ();
                        Rotation (list, rotate);
                        Print (list,0);
                        break;
                }
              }
          }
    }
    public static void sort (int [] myArray)
    {
        for (int a=0;a<myArray.length;a++)
          {
            int spot = 0;
            for (int b=1;b<myArray.length-a;b++)
            {
                if ((myArray[b] > myArray[spot]))
                {
                    spot = b;
                }
            int e = myArray [spot];
            myArray [spot] = myArray [myArray.length-a-1];
            myArray [myArray.length-1-a] = e;
            }
        }
    }
    public static void Print (int [] myArray, int c)
    {
        if (c!= (myArray.length))
        {
            System.out.println (myArray [c]);
            Print (myArray, c+1);
        }
     }
     public static void PrintBackwards (int [] myArray, int c)
     {
         if (c!= (-1))
        {
            System.out.println (myArray [c]);
            PrintBackwards (myArray, (a-1));
        }
     }
     public static void Rotate (int [] myArray, int a)
     {
         int f = (myArray.length-1);
        if (a>0)
        {
             for (int waster=0;waster<a;waster++)
             {
                 int e=myArray [f];
                for (int d=waster;d>0;d--)
                {
                    myArray[d]=myArray[d-1];
                    myArray [0] = e;
                }
             }
        }
          else if (a<0)
          {
            for (int other=0;other>a;other--)
            {
                  int e=myArray[0];
                  for (int d=0;d<other;d++)
                  {
                      myArray [d] = myArray [d+1];
                      myArray [f] = e;

                  }
             }
         }
     }
}

Soooooo, anyone know what I'm doing wrong?
 
Last edited:

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
For the first one, you don't have a method for filling in the matrix.

For the second one, you never fill in your array with anything.

The rest, you gotta fix your tags before my eyes bleed.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
For problem 11 I have a lot of questions. First, are you supposed to sort the names, or should the order be the order you got them in from the file? Second, where are the options to:

c. Search for a name
d. Add to the list
e. Modify a name in the list
f. Delete from the list

Third, why did you write your own sort routine? (Arrays.Sort(), for instance, could be useful - if you really want to sort anything, that is.)

Fourth, it seems like any normal program would write the list to the file at the end. Is that something he wants?
 

Battoe

Member
Jan 11, 2011
43
0
0
Yeah, object oriented programming. How do you go about filling in the array and matrix?

We had to create our own sort routine.
 

Battoe

Member
Jan 11, 2011
43
0
0
I have everything but the last one working now. I don't need to do the other options I didn't put in already, forgot to edit the instructions when posting them.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
I'm not going to point out the exact errors.

9D: There's something wrong with the way you're accessing the array. The rest looks fine.

9E: You're going about this in an odd way. Consider that the entire point of the program is to track how often a specific set of numbers (2 through 12) show up in random rolls. I'll give you a hint... your array should be a constant amount regardless of what number the user enters.

In 9F, you're actually on the right track. Although, why'd you store each roll in an array when you never seem to use it beyond adding it into sumDice? You also print out the header each time :p.

11: You shouldn't be using some weird for loop to loop your program. For loops are intended to be done over a number of iterations where while (and do while) loops are intended on logical constraints. It is possible to use the for loop in a better way too...

for (int i = 0; i < 4; )
{
//blah
i = console.readInt();
//blah

}

I certainly do not recommend doing this as it's really... weird :p.

You're not populating your array. You need to do this from a file, so pretty much... just Google "Java file input" or something like that and check out a few examples. You'll probably just have a file that looks like this:

John Doe
Jane Doe
Billy Bob Joe Jack
Printing an array forward and backward... that's pretty easy stuff that you can probably handle. Rotating an array... just realize that you don't need to try some weird method of doing it with one array... use memory... it's there for us to fill up with worthless crap ;).
 

Battoe

Member
Jan 11, 2011
43
0
0
Yeah I figured out 9d and 9e. 9f was done already :p I got one point off, that's probably why haha. But 11 is where I'm still getting problems. I fixed the two compile time errors, now it just gives me odd lists of ints when I run it. Fixed code below:

Code:
public class Assignment11
{
    public static void main(String[] args)
    {
        EasyReader console = new EasyReader ();
          int x = 10;
          int list [] = new int [x];
          for (int y=0;y<=(x-1);++y)
          {
              list [y] = ((int)((Math.random()*100)+1));
              sort (list);
              boolean quit = false;
              while (!quit)
              {
                System.out.println ();
                System.out.println ("Menu");
                System.out.println ("1. Print Array Forward");
                System.out.println ("2. Print Array Backwards");
                System.out.println ("3. Rotate the Array");
                System.out.println ("4. Exit");
                System.out.println ();
                System.out.println ("Enter a number: ");
                int num = console.readInt ();
                console.readLine ();
                switch (num)
                {
                    case 0:
                        quit = true;
                        break;
                      case 1:
                        Print (list, 0);
                        break;
                      case 2:
                        PrintBackwards (list, list.length -1);
                        break;
                      case 3:
                        System.out.println ("Which integer would you like to rotate the array? ");
                        int rotate = console.readInt ();
                        Rotate (list, rotate);
                        Print (list,0);
                        break;
                }
              }
          }
    }
    public static void sort (int [] myArray)
    {
        for (int a=0;a<myArray.length;a++)
          {
            int spot = 0;
            for (int b=1;b<myArray.length-a;b++)
            {
                if ((myArray[b] > myArray[spot]))
                {
                    spot = b;
                }
            int e = myArray [spot];
            myArray [spot] = myArray [myArray.length-a-1];
            myArray [myArray.length-1-a] = e;
            }
        }
    }
    public static void Print (int [] myArray, int c)
    {
        if (c!= (myArray.length))
        {
            System.out.println (myArray [c]);
            Print (myArray, c+1);
        }
     }
     public static void PrintBackwards (int [] myArray, int c)
     {
         if (c!= (-1))
        {
            System.out.println (myArray [c]);
            PrintBackwards (myArray, (c-1));
        }
     }
     public static void Rotate (int [] myArray, int a)
     {
         int f = (myArray.length-1);
        if (a>0)
        {
             for (int waster=0;waster<a;waster++)
             {
                 int e=myArray [f];
                for (int d=waster;d>0;d--)
                {
                    myArray[d]=myArray[d-1];
                    myArray [0] = e;
                }
             }
        }
          else if (a<0)
          {
            for (int other=0;other>a;other--)
            {
                  int e=myArray[0];
                  for (int d=0;d<other;d++)
                  {
                      myArray [d] = myArray [d+1];
                      myArray [f] = e;

                  }
             }
         }
     }
}
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Well yeah... because you populate your array with a bunch of random integers :p.

Code:
list [y] = ((int)((Math.random()*100)+1));
 

Battoe

Member
Jan 11, 2011
43
0
0
No, I mean the first and last are random, everything in the middle are 0s. Forward looks like

1
0
0
0
0
0
0
0
0
0
51
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Two problems...

1) The way you're filling your array
2) Your sort algorithm is a bit off. Are you trying to implement a bubble sort?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
If you don't have to implement your own sort algorithm, don't! But since you saw my earlier question, I guess you have to.

If you have to implement your own simple sort algorithm, I suggest Insertion Sort. (While the next element is bigger than the element before where you're thinking about putting it, shift that element to the right one.) Although some people like Selection Sort. (Find the smallest element to the right, and swap it with this one.)

If you have to implement a fast sort with Java, I'd suggest Mergesort. (Iterators might help.)
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
He should just create a method that calls Array.sort() :D.

"Look, I implemented a sort!"

Most I've ever had to do Java-wise when it came to sorting was overriding the compareTo method! Although, that was for a PriorityBlockingQueue rather than an actual sort call.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
I can't stress this enough. Write a seperate method for filling/creating your array.
Your for-loop inside your main method is messing everything up. You are assigning values to each array element AND waiting for user event.

If you don't know how, take a look at one of your other methods, cut out the void and put in int[], then put in all the array creation code inside, then on your main you can do int[] myIntArray = myIntArratMethod(); and get rid of that for loop in main.