Problem 1 |
public void forwardAndReverse()
{
int[] nums = new int[6];
for(int i=1; i<=5; i++)
{
System.out.print("Enter a number: ");
nums[i]=in.nextInt();
}
System.out.println();
System.out.print("Reverse order: ");
for(int i=5; i>=1; i--)
System.out.print(" "+nums[i]);
System.out.println();
System.out.print("Original order: ");
for(int i=1; i<=5; i++)
System.out.print(" "+nums[i]);
} |
Problem 2 |
public void letterChecker()
{
String[] letters = new String[6];
String letterCheck;
int flag=0;
for(int i=1; i<=5; i++)
{
System.out.print("Enter a letter: ");
letters[i]=in.next();
}
System.out.println();
System.out.print("Enter a letter to check: ");
letterCheck=in.next();
for(int i=1; i<=5; i++)
{
if(letters[i].compareTo(letterCheck)==0)
{
System.out.println(letterCheck+" is in the original list.");
flag=1;
break;
}
}
if(flag==0)
System.out.println(letterCheck+" is not in the original list.");
} |
Problem 3 |
public void indexChecker()
{
int[] numbers = new int[6];
int position;
for(int i=1; i<=5; i++)
{
System.out.print("Enter a number: ");
numbers[i]=in.nextInt();
}
System.out.println();
do
{
System.out.print("Enter the position (0 to Quit): ");
position=in.nextInt();
if(position!=0)
System.out.println("The number "+ numbers[position]+ " is stored in that position.");
}
while(position!=0);
System.out.println("Good-bye.");
} |
Problem 4 |
public void indexChecker()
{
int[] numbers = new int[11];
int highest=1, lowest=100;
System.out.print("The numbers selected randomly are: ");
for(int i=1; i<=10; i++)
{
numbers[i]=(int)(Math.random()*100)+1;
System.out.print(numbers[i]+" ");
}
System.out.println();
for(int i=1; i<=10; i++)
{
if(highest<numbers[i])
highest=numbers[i];
if(lowest>numbers[i])
lowest=numbers[i];
}
System.out.println("The highest number selected: "+ highest);
System.out.println("The lowest number selected: "+ lowest);
} |
Problem 5 |
public void multiples()
{
int[] numbers = new int[21];
int entry,flag;
System.out.print("Numbers chosen: ");
for(int i=1; i<=20; i++)
{
numbers[i]=(int)(Math.random()*100)+1;
System.out.print(numbers[i]+" ");
}
System.out.println();
do
{
System.out.print("Enter a number (0 to quit): ");
entry=in.nextInt();
flag=0;
if(entry!=0)
{
System.out.print("Multiples of "+entry+" are: ");
for(int x=1; x<=20; x++)
{
if(numbers[x]%entry==0)
{
System.out.print(numbers[x]+" ");
flag++;
}
}
if(flag==0)
System.out.print("no multiples found for "+entry+".");
}
System.out.println();
}
while(entry!=0);
System.out.println("Good-bye.");
} |
Problem 6 |
public void randomSentence()
{
String[] words = new String[11];
int index;
for(int i=1; i<=10; i++)
{
System.out.print("Enter word "+i+": ");
words[i]=in.next();
}
System.out.println();
System.out.print("The nonsense sentence is: ");
for(int i=1; i<=4; i++)
{
index=(int)(Math.random()*10)+1;
System.out.print(words[index] + " ");
}
} |
Problem 7 |
public void tripleThreat()
{
int[] nums = new int[16];
int[] totals = new int[4];
int sum=0,t=0;
for(int r=5; r<=15; r+=5)
{
sum=0;
for(int c=r-4; c<=r; c++)
{
nums[c]=(int)(Math.random()*10)+1;
sum+=nums[c];
}
t++;
totals[t]=sum;
}
for(int i=15; i>=1; i-=5)
{
System.out.print("The run "+i/5+" numbers are: ");
for(int a=i-4; a<=i; a++)
{
System.out.print(nums[a]+" ");
}
System.out.print("for a total of " + totals[i/5]);
System.out.println();
}
} |
Problem 8 |
public void oddsAndEvens()
{
int[] nums = new int[11];
for(int r=1; r<=10; r++)
{
nums[r]=(int)(Math.random()*100)+1;
}
System.out.print("Even numbers: ");
for(int r=1; r<=10; r++)
{
if(nums[r]%2==0)
System.out.print(nums[r]+" ");
}
System.out.println();
System.out.print("Odd numbers: ");
for(int r=1; r<=10; r++)
{
if(nums[r]%2==1)
System.out.print(nums[r]+" ");
}
} |
Problem 9 |
public void rollingTheDice()
{
int[] rolls = new int[13];
int die1,die2,total;
for(int r=1; r<=1000; r++)
{
die1=(int)(Math.random()*6)+1;
die2=(int)(Math.random()*6)+1;
total=die1+die2;
rolls[total]++;
}
System.out.println("Point Roll Times Appeared");
for(int r=2; r<=12; r++)
{
System.out.println(r+" "+rolls[r]);
}
} |
Problem 10 |
public void analyzer()
{
int[] nums = new int[51];
int[] occurrences = new int[101];
int n,total=0,highest=0,lowest=999,range,most=0;
double average;
for(int r=1; r<=50; r++)
{
n=(int)(Math.random()*50)+1;;
nums[r]=n;
}
for(int r=1; r<=50; r++)
{
total+=nums[r];
}
average=total/50;
System.out.println("The average is "+average);
for(int r=1; r<=50; r++)
{
if(nums[r]>highest)
highest=nums[r];
if(nums[r]<lowest)
lowest=nums[r];
}
System.out.println("The highest number selected is "+highest);
System.out.println("The lowest number selected is "+lowest);
range=highest-lowest;
System.out.println("The range is "+range);
for(int r=1; r<=50; r++)
{
occurrences[nums[r]]++;
}
for(int r=1; r<=50; r++)
{
if(occurrences[r]>most)
most=occurrences[r];
}
System.out.println("The median is "+ most);
System.out.println();
for(int r=1; r<=50; r+=5)
{
total=0;
for(int i=r; i<=r+4; i++)
{
total+=occurrences[i];
}
System.out.print(r+"-"+(r+4)+" ");
for(int x=1; x<=total; x++)
System.out.print("*");
System.out.println();
}
} |
Problem 11 |
public void guessMyNumber()
{
int[] guesses = new int[11];
int secretNum,guess;
secretNum=(int)(Math.random()*100+1);
System.out.println(secretNum);
for(int turn=1; turn<=10; turn++)
{
System.out.print("Enter your guess (1-100): ");
guesses[turn]=in.nextInt();
if(turn>1)
{
for(int check=1; check<turn; check++)
{
if(guesses[check]==guesses[turn])
{
System.out.println("Wake up!");
break;
}
}
}
if(turn==10 && guesses[turn]!=secretNum)
System.out.println("You did not guess the secret number. It was "+secretNum+".");
else if(guesses[turn]>secretNum)
System.out.println(guesses[turn]+" is too high. Guess again");
else if(guesses[turn]<secretNum)
System.out.println(guesses[turn]+" is too low. Guess again.");
else
{
System.out.println(guesses[turn]+" is correct!");
break;
}
}
} |
Problem 12 |
public void repeatedNumber()
{
int[] nums = new int[52];
for(int i=1; i<=51; i++)
{
nums[i]=(int)(Math.random()*50+1);
System.out.print(nums[i]+" ");
if(i>1)
{
for(int check=1; check<i; check++)
if(nums[i]==nums[check])
{
System.out.println();
System.out.println(nums[i] + " was stored at position " +check+" and at position "+i);
i=99;
break;
}
}
}
} |
Problem 13 |
public void sortedNumbers()
{
int[] nums = new int[6];
int temp;
System.out.print("The original order: ");
for(int i=1; i<=5; i++)
{
nums[i]=(int)(Math.random()*10+1);
System.out.print(nums[i]+" ");
}
for(int i=1; i<=5; i++)
for(int j=i; j<=5; j++)
if(nums[i]>nums[j])
{
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
System.out.println();
System.out.print("From low to high: ");
for(int i=1; i<=5; i++)
{
System.out.print(nums[i]+" ");
}
} |
Problem 14 |
public void noRepeatsAllowed()
{
int[] nums = new int[11];
for(int i=1; i<=10; i++)
{
nums[i]=(int)(Math.random()*15+1);
if(i>1)
{
for(int check=1; check<i; check++)
{
if(nums[i]==nums[check])
{
i--;
break;
}
}
}
}
System.out.print("The numbers selected are: ");
for(int i=1; i<=10; i++)
{
System.out.print(nums[i]+" ");
}
} |
Problem 15 |
public void sortedNoRepeats()
{
int[] nums = new int[11];
int temp;
for(int i=1; i<=10; i++)
{
nums[i]=(int)(Math.random()*15+1);
if(i>1)
{
for(int check=1; check<i; check++)
{
if(nums[i]==nums[check])
{
i--;
break;
}
}
}
}
for(int i=1; i<=10; i++)
for(int j=i; j<=10; j++)
if(nums[i]<nums[j])
{
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
System.out.print("The numbers selected are: ");
for(int i=1; i<=10; i++)
{
System.out.print(nums[i]+" ");
}
}
|
|
|
|