Tutorial 5

Section A

Exercises

1.

// IntArray.java

// Chapter 5, Section B, Exercise 1

public class IntArray

{

public static void main (String args[]) throws Exception

{

int[] numbers = new int[5];

int i;

for (i = 0; i < 5; i++)

numbers[i] = i + 10;

for (i = 0; i < 5; i++)

System.out.print("" + numbers[i] + " ");

System.out.print("\n");

for (i = 4; i >= 0; i--)

System.out.print("" + numbers[i] + " ");

System.out.print("\n");

}

}

2.

// Choice.java

// Chapter 5, Section A, Exercise 2

public class Choice

{

public static void main (String args[]) throws Exception

{

char[] sizes = {'S','M','L','X'};

double[] prices = {6.99,8.99,12.50,15.00};

char selection;

int i;

System.out.print("Enter pizza size (S,M,L,X): ");

selection = (char)System.in.read();

System.in.read();

System.in.read();

for (i = 0; i < 4; i++)

if (selection == sizes[i]) {

System.out.println("The price is " + prices[i]);

return;

}

System.out.println("Invalid Entry");

}

}

3a-b.

// Taxpayer.java

// Chapter 5, Section A, Exercise 3

public class Taxpayer

{

private int social;

private double income;

Taxpayer(int s, double i)

social = s;

income = i;

}

public int getSocial()

{

return social;

}

public double getIncome()

{

return income;

}

}

// UseTaxpayer.java

// Chapter 5, Section A, Exercise 3a

public class UseTaxpayer

{

public static void main (String args[])

{

Taxpayer[] workers = new Taxpayer[10];

int i;

for (i = 0; i < 10; i++)

workers[i] = new Taxpayer(999999999,0);

for (i = 0; i < 10; i++)

System.out.println("Taxpayer " + i

+ " has SSN " + workers[i].getSocial()

+ " and an income of " + workers[i].getIncome());

}

}

// UseTaxpayer2.java

// Chapter 5, Section A, Exercise 3b

public class UseTaxpayer2

{

public static void main (String args[])

{

Taxpayer[] workers = new Taxpayer[10];

int i;

for (i = 0; i < 10; i++)

workers[i] = new Taxpayer(i + 1,(i + 1) * 10000.0);

for (i = 0; i < 10; i++)

System.out.println("Taxpayer " + i

+ " has SSN " + workers[i].getSocial()

+ " and an income of " + workers[i].getIncome());

}

}

4.

// Prices.java

// Chapter 5, Section A, Exercise 3

public class Prices

{

public static void main (String args[])

{

double[] prices = {2.34,7.89,3.55,6.99,8.99,2.39,

1.29,11.88,56.35,12.33,4.50,1.19,1.79,2.59,7.10,

9.99,4.55,12.39,4.44,5.55};

double average;

double total = 0.0;

int i;

for (i = 0; i < 20; i++) {

total += prices[i];

if (prices[i] < 5.00)

System.out.println(prices[i] + " is less than 5.00");

}

average = total / (i + 1);

System.out.println("\nThe average price is " + average + "\n");

for (i = 0; i < 20; i++) {

if (prices[i] > average)

System.out.println(prices[i] + " is greater than the average");

}

}

5a-c.

// Student.java

// Chapter 5, Section A, Exercise 5a-c

// code added in each exercise is commented

public class Student

{

static int nextStudent = 1;

private int studentNumber;

private char[] studentGrades = new char[5];

// added in exercise 5b

static char[] grades = {'A','B','C','D','F'};

static int[] points = {4,3,2,1,0};

private int[] studentPoints = new int[5];

Student()

{

studentNumber = nextStudent++;

}

public void setGrade(int sub, char grade)

{

studentGrades[sub] = grade;

// added in exercise 5b

for (int i = 0; i < 5; i++)

if (grade == grades[i])

studentPoints[sub] = points[i];

}

public int getNumber()

{

return studentNumber;

}

// printGrades() added in exercise 5b

public void printGrades()

{

System.out.println("Student number " + studentNumber);

int total = 0;

for (int i = 0; i < 5; i++) {

System.out.println("\t" + " grade " + (i + 1)

+ " is "

+ studentGrades[i]

+ " for "

+ studentPoints[i] + " points ");

total += studentPoints[i];

}

// added in exercise 5c

System.out.println("\tGrade point average is " + (double)(total/5.0));

}

}

// GradePoint.java

// Chapter 5, Section A, Exercise 5a-c

// GradePoint.java

// Chapter 5, Section A, Exercise 5a-c

public class GradePoint

{

public static void main (String args[]) throws Exception

{

char[] grades = {'A','B','C','D','F'};

int i;

Student[] theClass = new Student[10];

for (i = 0; i < 10; i++)

theClass[i] = new Student();

double[] classGPAs = new double[10];

char g;

int x, j;

boolean match;

for (i = 0; i < 10; i++) {

for (j = 0; j < 5; j++) {

System.out.print("Enter grade " + (j + 1)

+ " for student " + theClass[i].getNumber() + ": ");

g = (char)System.in.read();

System.in.read();

System.in.read();

match = false;

for (x = 0; x < 5; x++)

if (g == grades[x])

match = true;

if (match == false) {

--j;

System.out.println("Please reenter grade");

}

else

theClass[i].setGrade(j,g);

}

}

for (i = 0; i < 10; i++)

{

theClass[i].calcGPA();

classGPAs[i] = theClass[i].getGPA();

theClass[i].printGrades();

}

}

 

Section B

// Ex1_5b.java

// Chapter 5, Section B, Exercise 1

public class Ex1_5b

{

public static void main (String args[]) throws Exception

{

char[] vowels = {'a','e','i','o','u' };

int i = 0;

char g = ' ';

boolean isVowel = false;

System.out.print("Enter a character: ");

g = (char)System.in.read();

System.in.read();

System.in.read();

for (i = 0; i < 5; i++) {

if (g == vowels[i])

isVowel = true;

}

if (isVowel == true)

System.out.println(g + " is a vowel");

else

System.out.println(g + " is NOT a vowel");

}

}

2.

// Ex2_5b.java

// Chapter 5, Section B, Exercise 2

public class Ex2_5b

{

public static void main (String args[]) throws Exception

{

char[] characters = new char[40];

int i = 0, alphabet = 0;

for (i = 0; i < 40; i++)

characters[i] = (char)(i + 40);

for (i = 0; i < 40; i++) {

if (characters[i] >= 'A' && characters[i] <= 'z')

alphabet++;

System.out.print(characters[i] + (i != 39?", ":"\n"));

}

System.out.println(alphabet + " of the above are in the alphabet");

}

 

Section C

1.

// SortDouble.java

// Chapter 5, Section C, Exercise 1

public class SortDouble

{

public static void main(String[] args)

{

double[] nums = {2.0,50.1,33.2,22.1,5.3,4.5,

9.0,30.1,43.2,12.1,75.3,14.5,

1.0,10.1,83.2 };

int i;

for (i = 0; i < nums.length; i++)

System.out.print(nums[i] + " ");

System.out.println("\n");

// sort

bubbleSort(nums, nums.length);

for (i = 0; i < nums.length; i++)

System.out.print(nums[i] + " ");

System.out.println("\n");

}

public static void bubbleSort(double[] array, int len)

{

int a, b;

double temp;

int highSub = len - 1;

for (a = 0; a < highSub; ++a)

{

for (b = 0; b < highSub; ++b)

if (array[b] > array[b + 1])

{

temp = array[b];

array[b] = array[b + 1];

array[b + 1] = temp;

}

}

}

}

2a-e.

// HairSalon.java

// Chapter 5, Section C, Exercise 2

// code added in each exercise is commented

public class HairSalon

{

private double price;

private String service;

private int minutes;

HairSalon(String s, double p, int m)

{

price = p;

service = s;

minutes = m;

}

public double getPrice()

{

return price;

}

public String getService()

{

return service;

}

public int getMinutes()

{

return minutes;

}

}

// SortSalon.java

// Chapter 5, Section C, Exercise 2