Introduction To Scientific Programming Using Java: National Diploma in Computer Technology
Introduction To Scientific Programming Using Java: National Diploma in Computer Technology
NATIONAL DIPLOMA IN
COMPUTER TECHNOLOGY
Week 3: Practicals..........................................................................................................................................
Practical: Week7...........................................................................................................................................22
Lab 14.1........................................................................................................................................................38
Lab 14.2.......................................................................................................................................................40
Lab 14.3.......................................................................................................................................................40
Lab 14.4.......................................................................................................................................................42
Lab 15.1........................................................................................................................................................44
Lab 15.2........................................................................................................................................................45
a. Create a Java source file using a text editor e.g. note pad.
b. Know how to save a Java file using a text editor
The operating system that was used for the implementation of this practical is Windows Vista, if
the operating system in your institution is different, get the assistance of your Computer room
Lab manager.
/*
* HelloWorld.java
* Displays Hello world!!! to the output window
*
*/
i. Start a command prompt session and change to the directory / folder containing your
Java source file. I have assumed that the file was saved in folder named JavaCodes,
if the implementation on your computer is different substitute the folder name as
appropriate.
C:\Java Codes>_
ii. Once this is done activate the Java compiler to convert the source code to byte codes.
The Java compiler creates a corresponding file with an extension .class, e.g.
HelloWorld.class
Objectives
Key in the program below and answer the questions that follow by test running the program.
Note that the line numbers are not part of the program. This is the same program presented in the
lecture notes. Similar method will be applied for most of the practical work you will be required
to complete in the subsequent weeks.
1 /*
2 * HelloWolrdGUI.java 3 *
4 */
5
6
7 import javax.swing.JOptionPane;
8
9 public class HelloWorldGUI {
10 public static void main(String[] args) {
11 String msg = "Hello Wolrd"; 12 String ans ="";
13
14 JOptionPane.showMessageDialog(null, msg );
15
16 // accept the users name
17 ans = JOptionPane.showInputDialog( null, "Enter your Name Please" );
18
19 // say hello to the user
20 JOptionPane.showMessageDialog(null, "Hello " + ans );
21
22 } // end method main
23
24 } // end of class HelloWorldGUI
Questions:
a. Describe the output of the program
In this practical exercise we will create two Java programs in order to see the difference between
entering data from the windows prompt and using input/output dialog boxes. The first program is
presented below:
Lab 3.1
/*
* AddTwoNo.java
* Add any two integer numbers
*/
import java.util.Scanner;
Lab 3.2
/*
* AddTwoNoDialog.java
* Add any two integer numbers
*/
import javax.swing.JOptionPane;
// display output
JOptionPane.showMessageDialog( null, output, "Adding two integers",
JOptionPane.INFORMATION_MESSAGE );
Your first task is to enter the following program and compile it. Ensure that it is free from any
errors and then proceed to answer the following questions.
/*
* AverageThreeIntegers
import javax.swing.JOptionPane;
// Calculate sum
sum = firstNumber + secondNumber + thirdNumber;
// Calculate average
average = sum/3.0; // Build
output string and display output
result = "Average of " + firstNumber + ", " + secondNumber + " and " +
thirdNumber + " is = " + average;
a. Run the program using the integer numbers 3, 4 and 5 as input. What is the output?
b. Run the program use the integer numbers 9, 5 and 11 as input. What is the output? Is the
output integer or floating point?
c. Amend the program and alter the statement: average = sum/3.0;
Change the statement to average = sum/3; then re-run the program with the set of data
supplied in questions a and b. What do you observe?
d. Run the program using the integer values -3, 3 and 0. What is/are the output generated?
Lab 5.1
In this practical session you are expected to key in the programs presented and answer the
questions presented at the end.
/*
* Triangle.java
*
*/
import javax.swing.JOptionPane;
Lab 5.2
/*
* TriangleTest.java
*
*/
import javax.swing.JOptionPane;
Lab 6.1
In this practical session you are required to enter the program listing below and then create an
html file to execute the JApplet. Thereafter answer the questions that follow:
/*
* MyFirstApplet.java
*
*/
Tasks:
a. Respond to the dialog boxes as they are being displayed.
b. When the paint() method prompt is displayed, does the next displayed when you respond
to the dialog box?
c. Click the close button on the applet, what happened now?
d. Click the close button to end the applet program.
Lab 6.2
/*
* SumTwoNumbers.java
*
*/
import javax.swing.JApplet;
import java.awt.Graphics; import
javax.swing.JOptionPane;
public class SumTwoNumbers extends JApplet {
String input;
String output;
String output;
output = "The Sum is: " + sum;
// Draw rectangle
g.drawRect( 15, 10, 270, 20 );
Listing 6.2
Task:
a. Key in the program above – listing 6.2
b. Compile and ensure that it error free.
c. Create an appropriate html file and run the program
d. Using the appletviewer execute the file – to do this type: appletviewer
SumTwoNumbers
What differences do you observed from the execution of the applet using the
appletviewer compared to using a web browser?
Lab 7.1
/*
* Guess.java
*
*/
int guessedNumber;
if(guessedNumber != randomNumber )
Questions:
a. Compile the program and make sure it is error free.
b. What is the program doing?
c. Amend the program such that it allow the user to guess integer numbers from 10 to 20.
Specific Objectives:
Lab 8.1
/*
* TenNos.Java
* Generates the First Ten Numbers and Calculate their Sum
*
*/
// Generate numbersn
while(counter <= n ){
output.append( counter + "\n" ); // add numbers to the JTextarea
sum += counter; // calculate sum
counter++; // increament counter by one
} // end while i <= n
Questions:
a. Enter the program above, compile it and ensure that it is error free.
b. What does the program do?
c. Alter the statement JTextArea output = new JTextArea( 5, 20 ); such that the actual
parameter 5 and 20 becomes 20 and 5. Compile and run the program. What is the effect
of the alteration?
Specific Objectives:
In this practical session will be required to enter the program below test it and answer the
questions beneath the codes. You are advised to enter the codes in week 9 of the lecture notes
and test them as well.
Lab 9.1a
/*
* RecursiveNos.java
*
*/
/*
* RecursiveNosTest.java
*
*/
Questions:
a. What does the program do?
b. Write the instruction: myNumbers.display1To10(-2);
What do you observe? Make suitable suggestions and implement same based on your
findings.
Specific Objectives:
10.2
Lab 10.3
Specific Objectives:
Lab11.1
Enter the program below and answer the questions hat follow:
Questions:
a. What is the output of the program?
b. Re-write the program such that the values generated will be in reverse order.
Lab 11.2
// Fig. 7.3: InitArray.java
// Initializing the elements of an array with an array initializer.
public class InitArray
{ public static void main( String
args[] ) {
// initializer list specifies the value for each element
int array[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
Questions:
a. What is the output of the program?
Specific Objectives:
Lab 11.1
Enter the program below and answer the questions listed below.
Questioins:
a. What is the output of the program.
Specific Objectives:
Lab 13.1
Lab 13.2
Specific Objectives:
In this practical session we will develop an Employee inheritance hierarchy; this will involve us
writing several programs. At the end of this practical, you will be required to develop an
inheritance hierarchy of your choice and submit the full working version.
Lab 14.3
Specific Objectives:
Lab 15.1
Enter the programs below and compile them until they are error free. At the end of this
practical session, you will be expected to apply the skills acquired and develop Java
programs implementing polymorphic principles.
1 // Fig. 15.1: PolymorphismTest.java
2 // Assigning superclass and subclass references to superclass and 3
// subclass variables.
4
5 public class PolymorphismTest
6 {
7 public static void main( String args[] )
8 {
9 // assign superclass reference to superclass variable
10 CommissionEmployee3 commissionEmployee = new CommissionEmployee3(
11 "Sue", "Jones", "222-22-2222", 10000, .06 ); 12
13 // assign subclass reference to subclass variable
14 BasePlusCommissionEmployee4 basePlusCommissionEmployee =
15 new BasePlusCommissionEmployee4( 16
"Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
17
18 // invoke toString on superclass object using superclass variable 19
System.out.printf( "%s %s:\n\n%s\n\n",
20 "Call CommissionEmployee3's toString with superclass reference ",
21 "to superclass object", commissionEmployee.toString() );
22
23 // invoke toString on subclass object using subclass variable 24
System.out.printf( "%s %s:\n\n%s\n\n",
25 "Call BasePlusCommissionEmployee4's toString with subclass", 26
"reference to subclass object",
27 basePlusCommissionEmployee.toString() );
28
29 // invoke toString on subclass object using superclass variable
Lab 15.2
1 // Fig. 15.4: Employee.java
2 // Employee abstract superclass. 3
4 public abstract class Employee
5 {
6 private String firstName;
7 private String lastName;
8 private String socialSecurityNumber;
9
10 // three-argument constructor
11 public Employee( String first, String last, String ssn )
12 {
13 firstName = first;
14 lastName = last;
15 socialSecurityNumber = ssn;
16 } // end three-argument Employee constructor
17
18 // set first name
19 public void setFirstName( String first )
20 {
21 firstName = first;
22 } // end method setFirstName
23
24 // return first name
25 public String getFirstName()
26 {
27 return firstName;
28 } // end method getFirstName
29
30 // set last name
31 public void setLastName( String last )
32 {
33 lastName = last;
34 } // end method setLastName
35
36 // return last name
37 public String getLastName()
38 {
39 return lastName;
40 } // end method getLastName
41
42 // set social security number
43 public void setSocialSecurityNumber( String ssn ) 44 {
45 socialSecurityNumber = ssn; // should validate