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.
Saturday, October 30, 2010
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!
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
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;
}
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)
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)
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
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
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
}
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
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
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
Monday, September 13, 2010
Useful Algorithms/Methods
Returns true if candidate is a factor of number
boolean isFactor(int number, int candidate){
if (number % candidate == 0 )
return true;
else
return false;
}
Returns the sum of the factors of number that are less than the number
int sumOfFactorsBelowNumber(int number){
int result=0;
for (int candidate = 1; candidate < number/2; candidate = candidate+1){
if (isFactor(number,candidate))
result = result + candidate;
}
return result;
}
Returns the count of factors of n
int countFactors(int n) {
int count=0;
for (int x=1; x<=n; x++){
if (isFactor(n,x))
count = count + 1;
}
return count;
}
Returns the count of factors of n
int countFactorsVersion2(int n) {
int count=1;
for (int x=1; x<=n/2; x++){
if (n >1)
if (isFactor(n,x))
count = count + 1;
}
return count;
}
Determining if an integer is a perfect number
An integer is a perfect number if the sum of its factors that are less than itself is equal to itself.
For example:
6 is a perfect number. This is because the factors of 6 that are less than 6 are 1, 2 and 3 and 1+2+3=6.
8 is NOT a perfect number because the factors of 8 that are less than 8 are 1,2,and 4 and 1+2+4 is NOT equal to 8.
28 is a perfect number because the factors of 28 that are less than 28 are 1,2,4,7,and 14 and 1+2+4+7+14 is equal to 28.
1. Read the integer n.
2. Get the sum of the factors of n that are less than n
3. If the sum of factors less than n is equal to n then n is a perfect number
For example:
6 is a perfect number. This is because the factors of 6 that are less than 6 are 1, 2 and 3 and 1+2+3=6.
8 is NOT a perfect number because the factors of 8 that are less than 8 are 1,2,and 4 and 1+2+4 is NOT equal to 8.
28 is a perfect number because the factors of 28 that are less than 28 are 1,2,4,7,and 14 and 1+2+4+7+14 is equal to 28.
1. Read the integer n.
2. Get the sum of the factors of n that are less than n
3. If the sum of factors less than n is equal to n then n is a perfect number
Determining if x is a factor of y
If x is a factor of y then the remainder if y is divided by x is zero. Hence, the following is an algorithm for showing the factors of a number.
1. Read the number
2. Let candidate = 1
3. if number modulo candidate equals zero then print candidate (because candidate is a factor)
4. Let candidate be increased by 1
5. if candidate <= number then repeat from step 3
NOTES:
a. Recall that the modulo operator in java is symbolized b %. ( i.e. x%y returns the remainder when x s divided by y)
b. The algorithm can be implemented by a for loop, while loop or do-while loop
c. It is best if the program implementation follows the topdown approach( a.k.a. Stepwise refinement)
1. Read the number
2. Let candidate = 1
3. if number modulo candidate equals zero then print candidate (because candidate is a factor)
4. Let candidate be increased by 1
5. if candidate <= number then repeat from step 3
NOTES:
a. Recall that the modulo operator in java is symbolized b %. ( i.e. x%y returns the remainder when x s divided by y)
b. The algorithm can be implemented by a for loop, while loop or do-while loop
c. It is best if the program implementation follows the topdown approach( a.k.a. Stepwise refinement)
Sample Code for Multiplication Table Generation
// by Dale D. Miguel
import java.util.*;
public class MTable {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int start, increment, rows, columns;
System.out.println("Multiplication Table Generation Program.");
do{
System.out.print("Start Value: ");
start = Integer.parseInt(kbd.nextLine());
System.out.print("Increment: ");
increment = Integer.parseInt(kbd.nextLine());
rows = readPositiveInteger("Enter the number of rows");
columns = readPositiveInteger("Enter the number of columns");
if (columns > 10){
System.out.println("The screen cannot accomodate more than 10 columns.");
System.out.println("It is suggested that you generate more than one multiplication tables.");
System.out.println("Please use at most 10 columns for each table.");
}
} while (columns > 10);
showMultiplicationTable(start, increment, rows, columns);
System.exit(0);
}
public static void showMultiplicationTable(int start, int step, int rows, int columns){
int ctrCol=0, ctrRow=0;
System.out.println(" M U L T I P L I C A T I O N T A B L E");
int colVal = start, rowVal=start;
//generate and print the first row
for (ctrCol= start; ctrCol< start+step*columns; ctrCol=ctrCol+step){
System.out.printf("%6d", ctrCol);
}
System.out.println();
//generate and print second row, third row, fourth row and so on.
for (ctrRow =start+step; ctrRow rowVal = ctrRow;
for ( ctrCol= start ; ctrCol< start + step*columns; ctrCol = ctrCol+step){
if (ctrCol == start )
colVal = 1;
else
colVal = ctrCol;
System.out.printf("%6d", rowVal*colVal);
}
System.out.println();
}
return;
} //end of showMultiplicationTable method
public static int readPositiveInteger(String prompt){
Scanner kbd = new Scanner(System.in);
int result=0;
do {
System.out.print(prompt + ": ");
result = Integer.parseInt(kbd.nextLine());
if (result <1)
System.out.println("Negative Value not allowed! Please try again.");
}while (result < 1);
return result;
}
}// end of class
import java.util.*;
public class MTable {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int start, increment, rows, columns;
System.out.println("Multiplication Table Generation Program.");
do{
System.out.print("Start Value: ");
start = Integer.parseInt(kbd.nextLine());
System.out.print("Increment: ");
increment = Integer.parseInt(kbd.nextLine());
rows = readPositiveInteger("Enter the number of rows");
columns = readPositiveInteger("Enter the number of columns");
if (columns > 10){
System.out.println("The screen cannot accomodate more than 10 columns.");
System.out.println("It is suggested that you generate more than one multiplication tables.");
System.out.println("Please use at most 10 columns for each table.");
}
} while (columns > 10);
showMultiplicationTable(start, increment, rows, columns);
System.exit(0);
}
public static void showMultiplicationTable(int start, int step, int rows, int columns){
int ctrCol=0, ctrRow=0;
System.out.println(" M U L T I P L I C A T I O N T A B L E");
int colVal = start, rowVal=start;
//generate and print the first row
for (ctrCol= start; ctrCol< start+step*columns; ctrCol=ctrCol+step){
System.out.printf("%6d", ctrCol);
}
System.out.println();
//generate and print second row, third row, fourth row and so on.
for (ctrRow =start+step; ctrRow
for ( ctrCol= start ; ctrCol< start + step*columns; ctrCol = ctrCol+step){
if (ctrCol == start )
colVal = 1;
else
colVal = ctrCol;
System.out.printf("%6d", rowVal*colVal);
}
System.out.println();
}
return;
} //end of showMultiplicationTable method
public static int readPositiveInteger(String prompt){
Scanner kbd = new Scanner(System.in);
int result=0;
do {
System.out.print(prompt + ": ");
result = Integer.parseInt(kbd.nextLine());
if (result <1)
System.out.println("Negative Value not allowed! Please try again.");
}while (result < 1);
return result;
}
}// end of class
Subscribe to:
Comments (Atom)