Showing posts with label Topic 03. Show all posts
Showing posts with label Topic 03. Show all posts

Tuesday, October 10, 2006

Displaying of Multi-lines

/******
This example shows how to display multiple lines in a dialog box
******/

import javax.swing.*;

public class DisplayMultipleLinesOnJOptionPane {
public static void main(String[] args) {

String inputStr;

inputStr = JOptionPane.showInputDialog(null,
"What drink you want?\n1)Coffee\n2)Tea\n3)Coke");

// Convert the input to an int
int drink = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"How many cups?");

int cups = Integer.parseInt(inputStr);

double costofdrink=0;

switch(drink){
case 1:
costofdrink = 0.50;
break;
case 2:
costofdrink = 0.60;
break;
case 3:
costofdrink = 1.00;
break;
} //end drink
JOptionPane.showMessageDialog(null,
"You ordered " + cups
+ " of drinks.\n"
+ "Total cost is : "+
+ costofdrink*cups);
} //end main
} //end class

Friday, October 06, 2006

Practical 3 Qns. 6

import javax.swing.JOptionPane;

public class Numnumdays {
public static void main(String[] args) {

int numdays = 0, month, year;
String monthname = "";
String input;

input =JOptionPane.showInputDialog(null,"Please enter the year","Year",JOptionPane.QUESTION_MESSAGE)
year = Integer.parseInt(input);

input = JOptionPane.showInputDialog(null,"Please enter the month of the year","Month",JOptionPane.QUESTION_MESSAGE);
month = Integer.parseInt(input);

switch (month){

case 1: numdays = 31;
monthname = "January";
break;

case 2:
if ((year % 4 == 0 && year % 100 != 0) year % 400 == 0)
numdays = 29;
else
numdays = 28;
monthname = "February";
break;

case 3: numdays = 31;
monthname = "March";
break;

case 4 : numdays = 30;
monthname = "April";
break;

case 5: numdays = 31;
monthname = "May";
break;

case 6: numdays = 30;
monthname = "June";
break;

case 7: numdays = 31;
monthname = "July";
break;

case 8: numdays = 31;
monthname = "August";
break;

case 9: numdays = 30;
monthname = "September";
break;

case 10: numdays = 31;
monthname = "October";
break;

case 11: numdays = 30;
monthname = "November";
break;

case 12: numdays = 31;
monthname = "December";

}

JOptionPane.showMessageDialog(null,"In " + monthname + " " + year + ", there are " + numdays + " numdays","No. of numdays",JOptionPane.INFORMATION_MESSAGE);

} // end main
} // end class

Prac 3 Question 3



// Question 03 Practical 3
import javax.swing.*;
public class CheckNumber {
public static void main(String[] args) {
String inputStr;
inputStr = JOptionPane.showInputDialog(null,
"Enter Number");
int num = Integer.parseInt(inputStr);
if (num%5== 0 && num%6==0)
JOptionPane.showMessageDialog(null,
num + " is divisible by both 5 and 6");
else if (num%5==0 ^ num%6==0)
JOptionPane.showMessageDialog(null,
num + " is divisible by either 5 or 6, but not both");
else
JOptionPane.showMessageDialog(null,
num + " is NOT divisible by 5 or 6");
} //end main
} //end class

Thursday, October 05, 2006

Pracitical 3 Qns.7 (Optional)

For those who have tried Practical 3 Qns.7, this is my solution just to show you how to make use of switch statement on JAVA.


import javax.swing.JOptionPane;

public class GradeModSwitch {
public static void main(String[] args) {

char grade;
int score;

score= Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your score","Enter Score", JOptionPane.QUESTION_MESSAGE));

switch (score / 10){ // this can also accept expression
case 10:grade = 'A';
break;
case 9: grade = 'A';
break;
case 8: grade = 'A';
break;
case 7: grade = 'B';
break;
case 6: grade = 'C';
break;
case 5: grade = 'D';
break;
default: grade = 'F';
}

JOptionPane.showMessageDialog(null,"Your grade is " + grade,"Grading System",JOptionPane.INFORMATION_MESSAGE);

} // end main
} // end class