Saturday, October 30, 2010

Pointer for those who will take the completion exam

You will be required to write a complete program.

Try developing simple programs on your own.

Possible problems.
1. Write a program that will generate a typical calendar for a year that is entered at run time.

2. Write a program for a simple text based calculator. The program should aid the user in adding, subtracting, dividing and multiplying two numbers.

3. Write a program that will aid the user to compute grades. At run time, the computer will prompt for the perfect class standing, the perfect exam score, the user's class standing and the user's exam score. The program will compute the grade corresponding to the user's class standing and the grade corresponding to the user's exam score by using an appropriate formula ( ex. grade = score/perfect_score * 50 +50). The user's grade is the average of the grade corresponding to the class standing and the grade corresponding to the exam score.
The process of accepting scores and computing the corresponding grade should be repeated as long as the user does not want to end.

Tuesday, October 26, 2010

Message to those who were given INC/NFE marks

Possible Reason(s) for the INC/NFE
1. Scores earned did not warrant a passing grade (70-72.5)
2. Term project was not submitted
3. A Quiz/ An Exam was not taken

Requirement(s) for completion (in case you are interested)
1. If you have not submitted the term project, you need to upload your file through the LateProjects folder under the Assignment box of our course area in the SLU LMS.

2. Prepare for a removal exam. Note that for you to pass, you need to perform excellently in the removal exam.

3. Apply for completion ASAP. I will be in school on the first day of classes to receive your applications for completion.

Good Luck and Regards!

Sunday, September 26, 2010

Simple H1N1 diagnostic program

import java.util.Scanner;
public class ClearanceFromH1N1{
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
char a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11;

System.out.print("Do you have fever? : ");
a1 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you have colds? : ");
a2 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you have cough? : ");
a3 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you have sore throat? : ");
a4 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you experience vomiting or nausea? : ");
a5 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you have diarrhea? : ");
a6 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you have headache? : ");
a7 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you have muscle or joint pains? : ");
a8 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Do you experience fatigue? : ");
a9 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Are you lacking in appetite? : ");
a10 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

System.out.print("Did you have contact with a person with swine flu? : ");
a11 = kbd.next().charAt(0); // Use Keyboard.readChar(); if you are using Keyboard.java

if (a1=='y' || a2=='y' ||a3=='y' || a4=='y'||a5=='y' || a6=='y'||a7=='y' || a8=='y'||a9=='y' || a10=='y' || a11=='y') {
System.out.println("Please have yourself checked at the medical clinic. ");;
}
else
{
System.out.println("You are okay for now. Always take necessary precautions. ");
System.out.println("Enjoy your lessons.....");
}
System.exit(0);
}// end of main
} // end of class

Wednesday, September 22, 2010

Determining the average of the elements of an array of numbers

Algorithm
Given: an Array of numbers and the number of elements

1. declare a variable to hold the sum of all the elements and initialize it to 0.
2. declare a variable to hold the average of the elements
3. add the first element to sum
4. add the next element to sum
5. repeat from step 4 until all elements are added to sum
6. let average = sum divided by the number of elements
Note: The size of an array is returned when accessing the length attribute of an array in JAVA.

Implementation

double computeAverage(double[] array){
double sum=0;
double average = 0;
for (int x=0; x < array.length; x++)
sum = sum + array[x];
average = sum/array.length;
return average;
}

Determining the highest element of an array of numbers

Algorithm
Given: Array of doubles
1.Declare a variable, say lowest, that will hold the highest element.
2.Initialize the variable lowest by assigning th first element of the array to it
3.Check if the next element is greater than the current value stored in highest. If the next element is greater than the current value of highest, let the value of highest be replaced by the element.
4.Repeat from step 3 until all elements are checked.
5.Give the value of highest

Method ( Implementation of the algorithm in a method)

double findHighestElement(double[] givenArray) {
double highest = givenArray[0];
for (int index=1; index < givenArray.length; index += 1) {
if (givenArray[index] > highest )
highest = givenArray[index];
}
return highest;
}

Tuesday, September 21, 2010

Determining the lowest element of an array

Algorithm

Given: Array of integers
1.Declare a variable, say lowest, that will hold the lowest element.
2.Initialize the variable lowest by assigning th first element of the array to it
3.Check if the next element is lesser than the current value stored in lowest. If the next element is lesser than the current value of lowest, let the value of lowest be replaced by the element.
4.Repeat from step 3 until all elements are checked.
5.Give the value of lowest

Method ( Implementation of the algorithm in a method)

int findLowestElement(int[] givenArray) {
int lowest = givenArray[0];
for (int index=1; index < givenArray.length; index += 1) {
if (givenArray[index] < lowest )
lowest = givenArray[index];
}
return lowest;
}

Friday, September 17, 2010

Program with Arrays (Please try and trace)

import java.util.Scanner;

public class GradeCalculatorUsingArrays {
static Scanner keyboard=new Scanner(System.in);

public static void main(String[] args) {
int[] quizScore; // array to hold quiz scores of students
int[] examScore; // array to hold exam scores of students
String[] names; // array to hold names of students
int perfectQuizScore; // variable to hold perfect quiz score
int perfectExamScore; // variable to hold perfect exam score
double[] quizGrade; // array to hold quiz grades of students.
double[] examGrade; // array to hold examination grades of students
double[] subjectGrade; // array to hold subject grades of students
int size;

/**
*Accept the number of students( class size).
*The class size must be greater than 0.
**/
do {
System.out.print("Enter the number of students: ");
size = Integer.parseInt(keyboard.nextLine());
if (size <0)
System.out.println("The number of students must be greater than 0.");
} while (size < 0);

// Instantiate arrays, the number of cells for each array must be equal to size
quizScore = new int[size];
examScore = new int[size];
names = new String[size];
quizGrade = new double[size];
examGrade = new double[size];
subjectGrade = new double[size];

/* Accept the perfect quiz score. The perfect score must be greater than 0.
*The do while construct enables the computer to let the user re-enter
*a perfect score in case the value entered is invalid.
**/
do {
System.out.print("Enter the Perfect Quiz Score : ");
perfectQuizScore = Integer.parseInt(keyboard.nextLine());
if (perfectQuizScore <= 0){
System.out.println("Invalid value! The perfect score must be greater than 0.");
}
} while (perfectQuizScore <= 0);

/* Accept the perfect exam score. The perfect score must be greater than 0.
*The do while construct enables the computer to let the user re-enter
*a perfect score in case the value entered is invalid.
**/
do {
System.out.print("Enter the Perfect Examination Score : ");
perfectExamScore = Integer.parseInt(keyboard.nextLine());
if (perfectExamScore <= 0){
System.out.println("Invalid value! Value must be greater than 0.");
}
} while (perfectExamScore <= 0);


// Accept names and scores and compute the grade of each student
for (int x=0; x System.out.print("Enter the name of student "+ (x+1) + ": ");
names[x] = keyboard.nextLine();

/* Accept the quiz score of a student.
*The quiz score must not be greater than the
*perfect quiz score.
*The do while construct enables the computer to let the user re-enter
*a score in case the value entered is invalid.
**/
do {
System.out.print("Enter the quiz score of "+ names[x] +" : ");
quizScore[x] = Integer.parseInt(keyboard.nextLine());
if (quizScore[x] > perfectQuizScore){
System.out.println("Invalid value! Score must not be greater than perfect score.");
}
} while (quizScore[x] > perfectQuizScore);

/* Accept the exam score.
*The exam score must not be greater than the
*perfect exam score.
*The do while construct enables the computer to let the user re-enter
*a score in case the value entered is invalid.
**/
do {
System.out.print("Enter the examination score of " + names[x] + " : ");
examScore[x] = Integer.parseInt(keyboard.nextLine());
if (examScore[x] > perfectExamScore){
System.out.println("Invalid value! Score must not be greater than perfect score.");
}
} while (examScore[x] > perfectExamScore);

quizGrade[x] = (double) quizScore[x]/perfectQuizScore*50 + 50;
examGrade[x] = (double) examScore[x]/perfectExamScore*50 + 50;
subjectGrade[x] = (quizGrade[x] + examGrade[x])/ 2;

// Check if subject grade is 100 then reset it to 99
if (subjectGrade[x] > 99)
subjectGrade[x] = 99;
//Check if subject grade is lesst than 65 then reset it to 65
if (subjectGrade[x] < 65 )
subjectGrade[x] = 65;

} // end of for

// Display the scores and grades of students
System.out.println("\n\nPerfect Quiz Score = " + perfectQuizScore);
System.out.println("\nPerfect Exam Score = " + perfectExamScore);
System.out.printf("%10s%10s%10s%10s%10s%10s\n","", "Quiz", "Exam", "Quiz", "Exam", "");
System.out.printf("%10s%10s%10s%10s%10s%10s\n","name", "Score", "Score", "Grade", "Grade", "Grade");
System.out.printf("%10s%10s%10s%10s%10s%10s\n","--------", "--------", "--------", "-------", "--------", "--------");
for (int y= 0; y < subjectGrade.length; y++){
System.out.printf("%10s%10d%10d%10.0f%10.0f%10.0f\n",names[y], quizScore[y], examScore[y], quizGrade[y], examGrade[y], subjectGrade[y]);
}
System.out.printf("%10s%10s%10s%10s%10s%10s\n","--------", "--------", "--------", "-------", "--------", "--------");
// Compute and show the average of the subject grades
// get the total of all the subject grades
double totalGrade=0; // variable to hold the sum of grades
for (int g=0; g totalGrade += subjectGrade[g];
}
double averageGrade = totalGrade/subjectGrade.length;
System.out.printf("%n%25s%5.2f%n", "Average Grade =", averageGrade);


// determine and show the lowest subject grade
double lowestGrade= subjectGrade[0]; // first grade is the lowest grade initially
for (int gr=1; gr if (lowestGrade > subjectGrade[gr])
lowestGrade = subjectGrade[gr];
}
System.out.printf("%n%25s%5.2f%n", "Lowest Grade =", lowestGrade);

// determine and show the highest subject grade
double highestGrade= subjectGrade[0]; // first grade is the highest grade initially
for (int h=1; h if (highestGrade < subjectGrade[h])
highestGrade = subjectGrade[h];
}
System.out.printf("%n%25s%5.2f%n", "Highest Grade =", highestGrade);


System.exit(0);
} // end of the main method
} // end of class