List of Chapters
Chapter 11: Using Arrays to Juggle Values
In this chapter:
import com.allmycode.dummiesframe.DummiesFrame;
public class Main {
public static void main(String args[]) {
DummiesFrame frame = new DummiesFrame("Hotel");
frame.addRow("Room number");
frame.setButtonText("Number of Guests in the Room");
frame.go();
}
public static int calculate(int room) {
int guests[] = {1, 4, 2, 0, 2, 1, 4, 3, 0, 2};
return guests[room];
}
}
public class Main {
public static void main(String[] args) {
double[] values = {3.4, 5.7, 9.8, 0.0, 10.1};
double total = 0;
for (double value : values) {
total += value;
}
System.out.println(total / values.length);
}
}
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JOptionPane;
import com.allmycode.dummiesframe.DummiesFrame;
public class FindVacancy {
static int guests[] = new int[10];
static int roomNum;
public static void main(String args[]) throws IOException {
Scanner diskScanner = new Scanner(new File("GuestList.txt"));
for (roomNum = 0; roomNum < 10; roomNum++) {
guests[roomNum] = diskScanner.nextInt();
}
diskScanner.close();
roomNum = 0;
while (roomNum < 10 && guests[roomNum] != 0) {
roomNum++;
}
if (roomNum == 10) {
JOptionPane.showMessageDialog(null, "Sorry, no v cancy");
} else {
DummiesFrame frame = new DummiesFrame("Vacancy");
frame.addRow("How many people for room " + roomNum + "?");
frame.setButtonText("Submit");
frame.go();
}
}
public static String calculate(int numberOfPeople) throws IOException {
guests[roomNum] = numberOfPeople;
String displayText = numberOfPeople + " booked in Room " + roomNum;
PrintStream listOut = new PrintStream("GuestList.txt");
for (roomNum = 0; roomNum < 10; roomNum++) {
listOut.print(guests[roomNum]);
listOut.print(" ");
}
listOut.close();
return displayText;
}
}
public enum Major {
COMPUTER_SCIENCE, MATHEMATICS, LITERATURE, PHYSICS, HISTORY
}
public class Student {
private String name;
private int id;
private Major major = null;
private double[] grades;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public Student(String name, int id, Major major) {
this.name = name;
this.id = id;
this.major = major;
}
public Student(String name, int id, Major major, double[] grades) {
this.name = name;
this.id = id;
this.major = major;
this.grades = grades;
}
public String getInfo() {
String info = "Name:" + name + "; ";
info += "ID:" + id + "; ";
info += "Major:" + (((major != null)?major:"None") + "; ");
info += "GPA:" + calculateGpa();
return info;
}
private double calculateGpa() {
double total = 0.0;
for (double grade : grades) {
total += grade;
}
System.out.println(total);
System.out.println(grades.length);
return total / grades.length;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Major getMajor() {
return major;
}
public void setMajor(Major major) {
this.major = major;
}
public double[] getGrades() {
return grades;
}
public void setGrades(double[] grades) {
this.grades = grades;
}
}
public class Main {
public static void main(String[] args) {
double grades1[] = {4.0, 3.5, 2.5, 4.0, 3.0};
Student student1 =
new Student("Barry", 111111, Major.COMPUTER_SCIENCE, grades1);
double grades2[] = {3.5, 3.3, 2.5, 4.0, 3.1};
Student student2 =
new Student("Harriet", 222222, Major.MATHEMATICS, grades2);
double grades3[] = {3.0, 2.0, 2.5, 4.0, 4.0};
Student student3 =
new Student("Jennie", 333333, Major.LITERATURE, grades3);
double grades4[] = {2.8, 3.8, 4.0, 3.5, 3.5};
Student student4 = new Student("Sam", 444444, Major.PHYSICS, grades4);
System.out.println(student1.getInfo());
System.out.println(student2.getInfo());
System.out.println(student3.getInfo());
System.out.println(student4.getInfo());
}
}
import static java.lang.System.out;
import java.util.Scanner;
public class Editor {
public static void main(String[] args) {
final int NUM_LINES = 5;
Scanner keyboard = new Scanner(System.in);
String[] lines = { "", "", "", "", "" };
while (true) {
out.println();
for (String line : lines) {
out.println("> " + line);
}
out.println();
int lineToReplace = 0;
do {
out.print("Line to replace (or -1 to quit): ");
lineToReplace = keyboard.nextInt();
keyboard.nextLine();
} while (lineToReplace < -1 || lineToReplace >= NUM_LINES);
if (lineToReplace == -1) {
break;
}
out.print("Type the new line: ");
lines[lineToReplace] = keyboard.nextLine();
}
keyboard.close();
}
}
public class Main {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage: java Main intValue1 intValue2 intValue3");
} else {
int largest = Integer.parseInt(args[0]);
int next = Integer.parseInt(args[1]);
if (next > largest) {
largest = next;
}
next = Integer.parseInt(args[2]);
if (next > largest) {
largest = next;
}
System.out.println(largest);
}
}
}
import static java.lang.System.out;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class Editor {
public static void main(String[] args) throws FileNotFoundException {
final int NUM_LINES = 5;
Scanner keyboard = new Scanner(System.in);
String[] lines = { "", "", "", "", "" };
Scanner fileIn = new Scanner(new File(args[0]));
for (int i = 0; i < 5; i++) {
lines[i] = fileIn.nextLine();
}
while (true) {
out.println();
for (String line : lines) {
out.println("> " + line);
}
out.println();
int lineToReplace = 0;
do {
out.print("Line to replace (or -1 to quit): ");
lineToReplace = keyboard.nextInt();
keyboard.nextLine();
} while (lineToReplace < -1 || lineToReplace >= NUM_LINES);
if (lineToReplace == -1) {
break;
}
out.print("Type the new line: ");
lines[lineToReplace] = keyboard.nextLine();
}
PrintStream fileOut = new PrintStream(args[1]);
for (int i = 0; i < 5; i++) {
fileOut.println(lines[i]);
}
keyboard.close();
fileIn.close();
fileOut.close();
}
}