Note: When creating a new project, it default program look something like this e.g:
public class Practical1 {
}
- Every Java application must have at least one class, Practical1 is the class name, also known as file name
- file name is the one you assign to it when you create a java project
- Java class name and file name must be the same and start with uppercase.
Note2: Each Java application must have the main method in the form of:
public static void main(String args[]){
or public static void main(String[]args)
Note3 : in order to display a single statement we use
System.out.println("Your Message here");
Practical 1 Introduction to Java
Compulsory programming exercises
Q1.1. Familiarize with IDE. Refer to Lab Guide1
Q1.2.
Write a Java application that display “W” in
large block letter which is made up of
lower case “w”.
Ans: public class MyFirstJava1 {
public static void main(String
args[]){
System.out.println("w W w");
System.out.println("
w W W
w");
System.out.println("
w W W
w");
System.out.println("
w W W
w");
System.out.println("
w W W
w");
System.out.println(" w w");
}
}
Q1.3.
Design a Java application to
display a message box which has the title "Welcome!" and message "I'm
Duke."(hint: refer to Listing 1.3)
Answer:
import javax.swing.JOptionPane; //declare at the beginning of the java class to use message Dialog box
public class Practical1 { // class name start with Uppercase
public static void main(String
args[]){ // main method
JOptionPane.showMessageDialog(null,"I'm Duke","Welcome",
JOptionPane.INFORMATION_MESSAGE);
//
JOptionPane.showMessageDialog(null,"MessageToBeDisplay","Title",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
//to release resource, at
the end of the java class
}
}
Then display “I'm Duke.” using System.out.println() method
Answer:
import javax.swing.JOptionPane;
public class MyFirstJava1 {
public static void main(String
args[]){
JOptionPane.showMessageDialog(null,"I'm Duke","Welcome",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("i'm
Duke!");
System.exit(0);
}
}
Practical 2 useful information:
Name variables must always start with lower case e.g int cat; double car;
Practical 2 Elementary Programming
Compulsory programming exercises
Refer to Lab Guide 2, design a program to get the square of number
11, then
display it.
Answer:
public class MyFirstJava1 {
public static void main(String
args[]){
int number1 = 11;
int displayresult = number1 * number1;
System.out.println(displayresult);
}
}
Modify it to get the
number from keyboard, then calculate and display the square of the entered
number.
Answer:
import javax.swing.JOptionPane;
public class MyFirstJava1 {
public static void main(String
args[]){
String
strIn = JOptionPane.showInputDialog(null,"Please Enter a Number", "Display the square of the entered
number", JOptionPane.QUESTION_MESSAGE);
int itIn = Integer.parseInt(strIn);
//if strIn is “2001”, itIn
will be 2001 , convert the string "strIn into integer "itIn"
/* float fltIn = Float.parseFloat(strIn);*/ //for float
/*double dblIn = Double.parseDouble(strIn);*/ // for double
int result = itIn
* itIn;
System.out.println(result);
}
}
Write a program to the prompt the user to enter a voltage and a
resistance for a resistor. Calculate and display the current. Note: current = voltage
/ resistance.
Answer:
import javax.swing.JOptionPane;
public class MyFirstJava1 {
public static void main(String
args[]){
String
strIn = JOptionPane.showInputDialog(null,"Please Enter the voltage", "Display the current", JOptionPane.QUESTION_MESSAGE);
String
strIn2 = JOptionPane.showInputDialog(null,"Please Enter the resistance", "Display the current", JOptionPane.QUESTION_MESSAGE);
int itIn = Integer.parseInt(strIn);
int itIn2 = Integer.parseInt(strIn2);//if strIn is “2001”, itIn will be 2001 , convert the string "strIn
into integer "itIn"
/* float fltIn = Float.parseFloat(strIn);*/ //for float
/*double dblIn = Double.parseDouble(strIn);*/ // for double
int result = itIn / itIn2;
System.out.println("Current
= voltage / Resistance = " +
result);
}
}
Q2.3 Write a
program that converts Fahrenheit to Celsius.
The formula for the conversion is as follows:
celsius
= (5/9) * (fahrenheit -
32)
Your program reads a Fahrenheit degree in double from the input dialog
box, then
converts it to Celsius and displays the result in a message dialog box.
Hint: In Java, 5/9 is
0, so you need to use 5.0/9 in the program to obtain the correct result.
Bonus programming exercises
Q2.4 Write a
program that prints a face, hopefully better looking than this one:
/ / / / /
|
Q Q |
{|
/\ |}
|
\__/ |
\--------/ Use comments to indicate when you print
the hair, ear, mouth, and so on. Hint:
to print out “\”, use “\\”
Q2.5 Write
a program that reads the subtotal and the gratuity rate, and computes the
gratuity and total. gratuity = subtotal * gratuity rate /100, total = subtotal
+ gratuity. e.g. if the user enters 10 for subtotal and 15 (percent) for gratuity
rate, the program displays 1.5 as gratuity and $11.5 as total.
No comments:
Post a Comment