Recall books have been assigned as reading and/or specified as references in Books24x7 (or Horstmann, from IU Bookstore).
Knowledge of parts 1, 2, 3, 4 is necessary to pass with a grade in the C range.
Add to that knowledge of part 5 plus at least a third of the homework turned in and you could get a grade in the B range.
Add to that knowledge of part 6 plus at least two thirds of the homework turned in and you could get an A or an A-.
If you have all the homework turned in perfectly you might already be in the A or A- range.
So these are the rules, and these are the available times for individual appointments.
This is the basic edit-compile-run-print loop. Example:
class One {
public static void main(String[] args) {
System.out.print("Boy, th");
System.out.print("is is g");
System.out.println("reat!");
}
}
The example below uses a scanner for user input, variables and simple arithmetic:
import java.util.*;
class Two {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("First number: ");
double a = s.nextDouble();
System.out.print("Second number: ");
double b = s.nextDouble();
System.out.print(a);
System.out.print(" + ");
System.out.print(b);
System.out.print(" = ");
System.out.print(a + b);
System.out.println();
System.out.println(a + " + " + b + " = " + (a + b));
String output = a + " + " + b + " = " + (a + b);
System.out.println(output);
}
}
Note we print the result in three different ways.
This program uses basic if statements to test a condition then take the correct course of action based on the test's result.
import java.util.*;
class Three {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n, m;
n = (int)(Math.random() * 100 - 50);
m = (int)(Math.random() * 100 - 50);
System.out.print("What is " + n + " + " + m + "? Answer: ");
int answer = s.nextInt();
if (answer == n + m) {
System.out.println("That's right! " + n + " + " + m + " = " + answer);
} else {
System.out.println("No, " + n + " + " + m + " = " + (n + m) + ", not " + answer);
}
}
}
Here's an example:
import java.util.*;
class Four {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter amount: ");
double initialAmount = s.nextDouble();
System.out.print("Enter interest rate: ");
double rate = s.nextDouble();
int yearsPassed = 0;
double currentAmount = initialAmount;
while (yearsPassed < 20 && initialAmount * 2 > currentAmount) {
double interest = rate * currentAmount / 100;
currentAmount = currentAmount + interest;
yearsPassed += 1;
}
String estimate = "\n(you need to wait longer to double up your money) ";
if (currentAmount >= 2 * initialAmount)
estimate = "\n(that's twice as much money as you started with) ";
System.out.print("In " + yearsPassed + " years your balance");
System.out.println(" will become " + currentAmount + estimate);
}
}
This programs uses loops to repeat a block of statements until we obtain the desired outcome.
import java.io.*;
import java.util.*;
class Five {
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner s = new Scanner(new File("numbers.txt"));
int length = 0;
String numbers = "";
while (s.hasNextInt()) {
int num = s.nextInt();
numbers += " " + num;
length += 1;
}
int[] nums = new int[length];
StringTokenizer t = new StringTokenizer(numbers);
for (int i = 0; i < length; i++)
nums[i] = Integer.parseInt(t.nextToken());
System.out.println(Arrays.toString(nums));
Arrays.sort(nums);
String sorted = Arrays.toString(nums);
System.out.println(sorted);
String fileName = "numbers-sorted.txt" ;
FileWriter writer = new FileWriter( fileName );
for (int i = 0; i < length; i++)
writer.write(nums[i] + "\n");
writer.close();
}
}
My test file for this program was numbers.txt shown below (note the format):
1 8
20 5 16
5
1
20
4
You'll have to run the program to see what it produces (output on screen and output in a text file).
The solution for Problem One can be given using two-dimensional arrays.
That aproach has the advantage of being able to build the pattern in more than one pass.
Here however we present a one-pass solution, that uses no storage, and just prints the pattern:
import java.util.*;
class Six {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("What size: ");
int size = s.nextInt();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++)
if (i == 0 || i == size-1 || i+j == size-1 || i == size/2 && j>=size/4 && j<= 3*size/4)
System.out.print("* ");
else
System.out.print(" ");
System.out.println();
}
}
}
For the second problem a two dimensional array of integers is indispensable:
class Magic {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int[][] sq = new int[n][n];
int i = sq.length - 1;
int j = sq.length / 2;
for (int k = 1; k <= n * n; k++) {
sq[i][j] = k;
int ni = (i + 1) % sq.length;
int nj = (j + 1) % sq.length;
if ( sq[ni][nj] != 0
|| (i == sq.length - 1 && j == sq.length - 1))
{
ni = i - 1;
if (ni < 0)
ni = sq.length;
nj = j;
}
i = ni;
j = nj;
}
for (i = 0; i < sq.length; i++) {
for (j = 0; j < sq[i].length; j++) {
String num = " " + sq[i][j];
System.out.print(num.substring(num.length() - 3) + " ");
}
System.out.println();
}
}
}
Note that the input (the size) comes from the command line in this case.