Assignment 1: CMPT 371



Assignment3: CMPT 101/104 Simon Fraser University, School of Computing Science

Due by Feb 23, 2004

1. (8 points) Exercise 7 page 193 of your text

State whether the following are valid switch statements. If not explain why. Assume that n and digit are int variables

a) switch(n 4.00 || GPA < 0.00)

{

System.out.println("Error in GPA in line " + numLine + "\n");

inputLine = inFile.readLine();

continue;

}

// if the student took course 1 (grade1>0) 1 add

// grade to meanGrade1 and to the correct mean

// meanGrade1Sex and increment appropriate counters

if( grade1 > 0)

{

meanGrade1 += grade1;

numGrade1++;

if( MaleorFemaleFlag == 'f')

{

meanGrade1Female += grade1;

numGrade1Female++;

}

else

{

meanGrade1Male += grade1;

numGrade1Male++;

}

}

// if the student took course 2 (grade2>0) 1 add

// grade to meanGrade2 and to the correct mean

// meanGrade2Sex increment appropriate counters

if( grade2 > 0)

{

meanGrade2 += grade2;

numGrade2++;

if( MaleorFemaleFlag == 'f')

{

meanGrade2Female += grade2;

numGrade2Female++;

}

else

{

meanGrade2Male += grade2;

numGrade2Male++;

}

}

if( GPA < minGPA)

{

minGPA = GPA;

}

if( GPA > maxGPA)

{

maxGPA = GPA;

}

meanGPA += GPA;

numGPA++;

if( MaleorFemaleFlag == 'f')

{

meanGPAFemale += GPA;

numGPAFemale++;

if( GPA < minGPAFemale)

{

minGPAFemale = GPA;

}

if( GPA > maxGPAFemale)

{

maxGPAFemale = GPA;

}

}

else

{

meanGPAMale += GPA;

numGPAMale++;

if( GPA < minGPAMale)

{

minGPAMale = GPA;

}

if( GPA > maxGPAMale)

{

maxGPAMale = GPA;

}

//read next line of data

}

inputLine = inFile.readLine();

}

// Calculate the mean grades and GPA's

// Before dividing by the number of students

// in the applicable group test to assure that

// that group has at least 1 member.

// If there are no memebers in the group the

// mean will be 0 (the initial value)

if( numGPA > 0)

{

meanGPA /= numGPA;

}

if(numGPAFemale > 0)

{

meanGPAFemale /= numGPAFemale;

}

if(numGPAMale >0)

{

meanGPAMale /= numGPAMale;

}

if(numGrade1 > 0)

{

meanGrade1 /= numGrade1;

}

if(numGrade1Male > 0)

{

meanGrade1Male /= numGrade1Male;

}

if(numGrade1Female > 0)

{

meanGrade1Female /= numGrade1Female;

}

if(numGrade2 > 0)

{

meanGrade2 /= numGrade2;

}

if(numGrade2Male > 0)

{

meanGrade2Male /= numGrade2Male;

}

if(numGrade2Female > 0)

{

meanGrade2Female /= numGrade2Female;

}

//Assemble and print the output table

System.out.println("\t\t\t\tCourse #1\t" +

"Course #2\tGPA");

System.out.println("Total Number of Students\t" +

numGrade1 + "\t\t" + numGrade2 + "\t\t" + numGPA);

System.out.println("Mean Grades\t\t\t" + meanGrade1 + "\t\t" + meanGrade2

+ "\t\t" + twoDecimal.format(meanGPA));

System.out.println("GPA range\t\t\t\t\t\t\t"

+ minGPA + " to " + maxGPA);

System.out.println("Number of Female Students\t" +

numGrade1Female + "\t\t" + numGrade2Female +

"\t\t" + numGPAFemale);

System.out.println("Mean Grades (female)\t\t" + meanGrade1Female + "\t\t" + meanGrade2Female

+ "\t\t" + twoDecimal.format(meanGPAFemale));

System.out.println("GPA range (female)\t\t\t\t\t\t"

+ minGPAFemale + " to " + maxGPAFemale);

System.out.println("Number of Male Students\t\t" +

numGrade1Male + "\t\t" +

numGrade2Male + "\t\t" +

numGPAMale);

System.out.println("Mean Grades (male)\t\t" +

meanGrade1Male + "\t\t" +

meanGrade2Male + "\t\t" +

twoDecimal.format(meanGPAMale));

System.out.println("GPA range (male)\t\t\t\t\t\t"

+ minGPAMale + " to " + maxGPAMale);

}

}

6. (30 points) Use a while loop to repeat the following series of steps until the user indicates that they do not wish to enter a new value of firstNum and secondNum. Each specifically described output should be on its own line.

a. Prompt the user for two integers firstNum and secondNum. firstNum must be less than secondNum. Data should be checked and rerequested if secondNum firstNum : all possible combinations of odd and even

input of some other string than yes or no (should rerequest whether the user wants to run

for another set of integers)

secondNum = firstNum odd and even

secondNum >= firstNum with secondNum –firstNum less that 26 and greater than 26

Detailed algorithm

g. Prompt the user for two integers firstNum and secondNum. firstNum must be less than secondNum. Data should be checked and rerequested if secondNum = secondNum)

{

System.out.print(" Input smallest integer: ");

System.out.flush();

inputLine = keyboard.readLine();

firstNum = Integer.parseInt(inputLine);

System.out.print(" Input largest integer: ");

System.out.flush();

inputLine = keyboard.readLine();

secondNum = Integer.parseInt(inputLine);

if(firstNum >= secondNum)

{

System.out.println(

"Largest integer was less than” +

+ " smallest integer"

+ " \nplease reenter your values \n\n");

}

}

System.out.println();

// Use a for loop to output odd integers between

// firstNum and secondNum and to calculate the sum of

// the squares

// of all the odd integers between

firstNum and secondNum

sumSq = 0;

System.out.println("Odd numbers between " + firstNum

+ " and " + secondNum + " are");

for(i=firstNum; i 0);

// Output thr sum of all positive integers ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download