Lab 1

Task 1: Understanding polymorphism and inheritance basics

  1. Write the following DBDemo.java, Person.java, Student.java, and GraduateStudent.java. Compile and run the program.

publicclass DBDemo {

publicstaticvoid main(String[] args){

m(new GraduateStudent());

m(new Student());

m(new Person());

m(new Object());

Student aS = new Student();

aS.setName("Bob Feiner");

System.out.println(aS.getName());

}

publicstaticvoid m(Object x){

System.out.println(x.toString());

}

}

publicclass Person {

private String name;

public String toString(){

return "Person";

}

publicvoid setName(String aName){

name = aName;

}

public String getName(){

return name;

}

}

publicclass Student extends Person{

public String toString(){

return "Student";

}

}

publicclass GraduateStudent extends Student{

}

Task 2: Compiling and Executing a Java Applet using Eclipse

Write a Java program named "HelloApplet.java". The java source code is shown below.

import javax.swing.*;

import java.awt.*;

public class HelloApplet extends JApplet{

public void paint(Graphics canvas){

canvas.drawString("Welcome to Java Programming!", 25,25);

}

}

Task 3: Modifying an Existing Java Program

1. Add source code to HelloApplet.java to display your name under the text "Welcome to Java Programming!".

2. Compile and run your modified program.

Task 4: Resolving Syntax Errors

  1. Write ErrorApplet.java. The source code is shown below. The working applet is shown in Figure 1 below.

import java.awt.*;

public class ErrorApplet extends JApplet {

public void paint(Graphics canvas){

canvas.drawLine(25, 25, 125,25)

canvas.drawString(A Graphical Applet!, 25, 50);

canvas.drawLine(25, 25, 125, 75);

}

Figure 1; An Applet to be developed

2. This program contains several syntax errors as well as one logic error. Compile the program and observe the error messages. Resolve the errors in the program. The compiler will produce the file ErrorApplet.class that contains the executable byte code when all the errors are resolved.

Task 5: Write Applet and Frame based Application in one program

  1. After you finish task 4. Write a Java program named “MyAppletFrame.java”. Copy the fixed program in task 3 to MyAppletFrame.java.
  2. Modify your program to run it as an applet as well as Frame based application.

Task 6:Variables and Expressions

Write a Java program named "Average3.java." The Java source code is shown below.

public class Average3 {

public static void main(String[] args) {

int num1 = 75;

int num2 = 137;

double average = (num1 + num2)/2;

System.out.print("The average of " + num1);

System.out.println(" and " + num2 + " is " + average);

}

}

  1. Compile and run "Average3.java." and observe the output. The program declares three variables; two variables hold integer numbers that have been provided within the program. and one variable of type double is declared to store the result of the calculation. The program then displays the average of the two numbers.
  2. Modify the program as follows (fix any syntax errors):
  3. Create an integer variable named num3.
  4. Assign the value of 265.25 to num3.
  5. Modify the average calculation to include num3.
  6. Modify the output statements as appropriate.
  7. Compile and run the program.

Task 7: Java’s String Class

  1. Write a Java program named "StringDemo.java." The Java source code is shown below.

import java.util.*;

public class StringDemo{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

String input;

System.out.print("Enter a string: ");

input = keyboard.next();

System.out.println("You entered: " + input);

}

}

  1. Modify the program as follows:
  2. Insert a line of code that calculates the length of the string the user entered and displays it. Insert your line of code after: System.out.println("You entered: " + input);

Task 8: Variables and Expressions

  1. Write a Java program named “Average3Gui.java”. The Java source code is shown below.

import javax.swing.*;

public class Average3Gui{

public static void main(String[] args)

{

String input;

input = JOptionPane.showInputDialog("Enter an integer:");

int num1 = Integer.parseInt(input);

input = JOptionPane.showInputDialog("Enter an integer:");

int num2 = Integer.parseInt(input);

double average = (num1 + num2)/2;

JOptionPane.showMessageDialog(

null, "The average is " + average);

}

}

  1. Modify the program as follows:
  2. Create a JOptionPane to prompt the user for the third number. Store the String result in the String variable named input. Change the title of the input dialog box to “Input Dialog” instead of default title “Input”. Also change the ? mark icon to ! (exclamation mark).
  3. Create an integer variable named num3.
  4. Add the command to parse the string input as an integer and store the result in num3.
  5. Modify the average calculation to include num3.
  6. Modify the output statements as appropriate.
  7. Compile and run the program.

1