- A web search. Answers vary.
- A Rot13 application:
import java.util.Scanner;
/**
* An application which writes, to System.out, the Rot13 encoding
* of System.in.
*/
public class Rot13Encoder {
/**
* Runs the application.
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
// Process each character of the line
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
char lower = Character.toLowerCase(c);
// Convert a..m <==> n..z
if ('a' <= lower && lower <= 'm') {
c += 13;
} else if ('n' <= lower && lower <= 'z') {
c -= 13;
}
System.out.print(c);
}
// Write a new line between each line of the input
System.out.println();
}
}
}
- The egg problem:
/**
* An application that prompts the user for a number of eggs, and
* reports this number in grosses and dozens.
*/
public class EggCountReporter {
/**
* Runs the application. If the user enters a nice number,
* the program writes a nice report. Otherwise it just
* throws an exception.
*/
public static void main(String[] args) {
String response = System.console().readLine("How many eggs ya got? ");
int eggs = Integer.parseInt(response);
int gross = eggs / 144;
int excessOverGross = eggs % 144;
int dozens = excessOverGross / 12;
int leftOver = excessOverGross % 12;
System.out.printf("You have %d gross, %d dozen, and %d\n",
gross, dozens, leftOver);
}
}
- The average finder:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* An application that solves a problem in David Eck's Java
* programming book. It reads the file "testdata.txt" which is
* assumed to have (at least) 4 lines. The first is the name of
* a student and the next three are integer scores for three
* exams. The program then writes a message to System.out saying
* what the student's average score is.
*/
public class AverageFinder {
/**
* Runs the application.
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("testdata.txt"));
String name = scanner.nextLine();
int score1 = scanner.nextInt();
int score2 = scanner.nextInt();
int score3 = scanner.nextInt();
double average = (score1 + score2 + score3) / 3.0;
System.out.printf("The average for %s is %1.1f\n", name, average);
}
}
- An application that finds a number with the most divisors:
/**
* An application that finds the number between 1 and 10000 with
* the most divisors. Adapted nearly verbatim from David J. Eck's
* solutions at http://math.hws.edu/javanotes/c3/ex2-ans.html.
*/
public class MaximumNumberOfDivisorsFinder {
public static void main(String[] args) {
// The number with the most divisors "so far" is 1
int numWithMax = 1;
// The most divisors found so far for any number
int maxDivisors = 1;
// Try each value from 2...
for (int n = 2; n <= 10000; n++) {
// Count the number of divisors for n
int divisorCount = 0;
for (int d = 1; d <= n; d++) {
if (n % d == 0) {
divisorCount++;
}
}
// If n has more than the maximum yet found, update what we know
if (divisorCount > maxDivisors) {
maxDivisors = divisorCount;
numWithMax = n;
}
}
// Do some fancy reporting
System.out.println("Among integers between 1 and 10000,");
System.out.println("The maximum number of divisors is " + maxDivisors);
System.out.println("A number with this many divisors is " + numWithMax);
}
}
- An application that prints some sales figures:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* An application which simulates a sales report from data from a file called
* "sales.dat". Each line of the file contains the name of a city, followed by a
* colon, followed by either a number giving the amount of sales in that city or
* by a message saying why the sales figure is not available. The program prints
* the total sales for all cities and the number of cites for which the figure
* was not available. Very loosely based on David J. Eck's solution at
* http://math.hws.edu/javanotes/c3/ex5-ans.html.
*/
public class SalesReporter {
/**
* The filename where we expect to see the data. In a real application,
* this would be a parameter.
*/
private static final String FILENAME = "sales.dat";
/**
* Runs the application.
*/
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File(FILENAME));
double salesTotal = 0;
int missingCount = 0;
// Read and process each line of the file.
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int colonIndex = line.indexOf(':');
if (colonIndex < 1) {
throw new Exception("Malformed line: [" + line + "]");
}
try {
// Take everything after the colon
String amountText = line.substring(colonIndex + 1);
Double amount = Double.parseDouble(amountText);
salesTotal += amount;
} catch (NumberFormatException e) {
// Not a double
missingCount++;
}
}
// Report
System.out.printf("Total sales: $%1.2f\n", salesTotal);
if (missingCount == 0) {
System.out.println("Data was received from all cities.");
} else {
System.out.printf("Number of cities with missing data: %d\n",
missingCount);
}
} catch (FileNotFoundException e) {
System.out.println("The file " + FILENAME + " is not found");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
- A checkerboard applet:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
/**
* An applet that draws an 8 x 8 checkerboard with red
* and black squares. Each squre is 20 pixels wide.
*/
public class CheckerboardApplet extends Applet {
private static int SIZE = 20;
private static int ROWS = 8;
private static int COLUMNS = 8;
public void paint(Graphics g) {
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
g.setColor(row % 2 == column % 2 ? Color.red : Color.black);
g.fillRect(row * SIZE, column * SIZE, SIZE, SIZE);
}
}
}
}