Wednesday, January 16, 2008

What is problem solving?

Define problems. What are formal and informal problems?

something which is difficult to answer or to deal with or understand

How do we solve problem for computers? What are the steps?

(1) Understanding the problem
(2) Making a plan of solution

(3) Carrying out the plan
(4) Looking back i.e. verifying


Consider the following problem. "You are requited to develop a program to calculate Grade Point Average (GPA) for a student". Analyze the problem. Devise the steps (algorithm) to solve it. Code & run the program.

import javax.swing.JOptionPane;
public class machintshCGPA{
public static void main(String[] args){

//declare variables and initial values
String SInput;
String MoreGrade;
char input;
char sign;
int lenSInput;
double total=0;
double average;
boolean Grade = true;
int subjectCounter=0;

//prompt for grade input
SInput = JOptionPane.showInputDialog(null,"Please enter the grade : ","Input",JOptionPane.QUESTION_MESSAGE);


while (Grade){
subjectCounter++;
lenSInput = SInput.length();
//change the grade letter to upper case
input = SInput.charAt(0);
input = Character.toUpperCase(input);

//grade letter
switch (input){
case 'A' :
total = total + 4;
break;

case 'B' :
total = total + 3;
break;

case 'C' :
total = total + 2;
break;

case 'D' :
total = total + 1;
break;

default :
total = total + 0;
break;
}

// grade sign (+/-)

if (lenSInput == 2){
sign = SInput.charAt(1);
switch (sign){
case '-':
total = total - 0.25;
break;

case '+':
total = total + 0.5;
break;

default:
total = total + 0;
}
}

else
total = total + 0;

//ask for more grades
MoreGrade = JOptionPane.showInputDialog(null,"Have more grade?\n[1 for Yes, 2 for No]\nGrades counted:"+subjectCounter,"Input",JOptionPane.QUESTION_MESSAGE);
if (Integer.parseInt(MoreGrade)==2){
Grade = false;
}

else
SInput = JOptionPane.showInputDialog(null,"Please enter the grade : ","Input",JOptionPane.QUESTION_MESSAGE);

}

//calculation
average = total / subjectCounter;


JOptionPane.showMessageDialog(null,"The average grade = "+average,"Average Grade",JOptionPane.INFORMATION_MESSAGE);
}
}

Output



































No comments: