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:
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:
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:
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:
Soooooo, anyone know what I'm doing wrong?
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 " + "% 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:
