Way to generate random numbers with one line of code:

 

http://www.cs.geneseo.edu/~baldwin/reference/random.html

 

import java.util.Random;

 

Random generator = new Random();

Or

Random generator = new Random(12345678);

 

int randomIndex = generator.nextInt( n );

 

int throw = generator.nextInt(6) + 1; // dice

 

Sample questions:

 

For studying, you will have under 10 questions similar to the ones on the two quizzes you took. You will also have a few problems to code. So please study from your quizzes and this guide.

 

  1. This never stops looping. Tell me what is wrong with this. (Please use the debugger while in class practicing.)

public class q1{

  public static void main(String[] args){

for (int count=0; count != 5; count++)
{

    if (count < 3)
     
System.out.println(count-3);

    else

       System.out.println(count+5);
count++;
}}}

 

  1. Write a Boolean expression that is true when a number is between (and not including) 7 and 10 or when the number is 33.

 

  1. Create a program with variable oneVal set to ABC and twoVal set to DEF. In your program, swap the variable values so that oneVal becomes DEF and twoVal becomes ABC. But, you cannot set oneVal = DEF or twoVal = ABC. Start with:

 

public class q3{

  public static void main(String[] args){

     String oneVal = “ABC”;

     String twoVal = “DEF”;

 

     // swap the values of oneVal and twoVal below:

 

   }}

 

  1. Create a program that asks the user for a quantity and prints out the price according to the table below:

Quantity Break

Unit price

Less than 5

$100

Greater than or equal to 5 and less than 50

$80

Greater than or equal to 50 and less than 100

$75

100 or more

$70

  

   For example:

       If the user enters 4, it should print 4 at 100 = $400.00

   Format the price as currency.

 

  1. List the test cases that would be good for the program above.

 

  1. Create a program that asks a user for a price and a discount percentage and a tax rate. Calculate the final price with the discount and tax rate applied and print the result. Format the final price as currency in your answer. Keep asking the user if they want to continue until they type no.  

 

  1. Create a program that asks the user for a name. Change that name to have every other letter capitalized and print it out. Keep asking the user for another name until they type END instead of a name.

 

  1. Throw 2 dice 10 times and print a running total. When you are done, print the final total, and then ask the user if they want to repeat. If they do, play another 10 times and give the total just for the last 10 times.

 

  1. Ask the user for all their grades (as numbers) and then print their average and the letter grade for that average at the end. Stop asking for new grades when they enter a negative grade. You can assign an A to everything 90 and above, a B to 80-89, a C to 70-79 and a D to 60-69 and an F below 60.

 

  1. Throw 2 dice as many times as you need to have the total sum of all dice become greater than 40. Print the total sum of all dice, and the number of times you threw the dice. Only print this total once.

 

 

---------------------------------------

  1. This question is harder than what I will be asking:

 

Draw a flow chart for the problem of graphing the function f(x) = (x - 4) (x - 5) + 1, for a range of values of x given by the user, as described below.

 

First, read in two integers, representing the range of values of x, i.e. read in the low value and the high value of x. For each value of x, compute y = (x - 4) * (x - 5) + 1, and print y asterisks. For example, if x = 2, then y = 7, so 7 asterisks should be drawn on the same line. If the user enters 2 and 7 as the low and high values of x respectively, then the output should look like this:

Enter the low value of x: 2
Enter the high value of x: 7
 
x f(x)
--------------------------------------------------
2 |*******
3 |***
4 |*
5 |*
6 |***
7 |*******

We require that the low and high values entered by the user satisfy low >= 0, low < 10, low < high, and that low and high are integers. Your program needs to enforce this.

After drawing the graph, ask the user if (s)he wants to continue, i.e. draw another graph.

  1. This is also harder:

Ask the user for a string that contains two colons inside it. Extract the part before the colon, in between the colons and after the last colon. So, if the user entered ABC:DEF:GHI, you would write first is ABC, second is DEF and Third is GHI. If the user doesn't include two colons, ask the user to enter again.

 

You will need to use String commands we have not used before, so you will have to read through the documentation on String to do this. Use the BlueJ help feature for this.