CSI 311L Lab 3, Spring 2012
American University of Science and Technology · Java Programming Laboratory · 4 pages · Dr. Saeed Raheel
More CSI 311L files
Show the text of this lab
American University of Science & Technology
Department of Computer Science
CSI 311L – Java Programming Lab
Spring 2011/2012
Lab Work 3
Problem1:
Trace the following java applications:
a-
public class Q1
{
public static void main (String a[] )
{
int x = 2, y = 3, z = 4;
z = ++x + y--;
if (x > 1)
if (y > 1)
System.out.println ('@');
else
System.out.println ('#');
for (int i = 1; i < z; i++)
{
for (int j = 1; j < x; j++)
System.out.print ('@');
System.out.println();
}
for (int k = 1; k < y; k++)
System.out.print ('#');
}
}
1
b-
public class Q2{
public static void main( String args[] )
{
if( true )
if( false )
System.out.println( 'a' );
else
System.out.println( 'b' );
}
}
c-
public class Q3{
public static void main( String args[] ){
int x = 352;
int y = 439;
boolean z;
if( y != x ){
z = false;
}
else{
z = true;
}
System.out.println( z );
}
}
d-
public class Q4{
public static void main (String args[])
{
boolean b = false;
int i = 1;
do{
i++;
b = !b;
}while( b );
System.out.println( i );
}
}
2
Problem 2:
Type the following java application then apply the following listed steps:
class Student{
private String Id;
private String name;
private double gpa;
public Student(String id, String nm, double g)
{
Id=id; name=nm;gpa=g;
}
public void print()
{
System.out.println("ID= "+ Id+"name = "+name+" gpa ="+gpa );
}
}
public class Test{
public static void main (String args[])
{
Student s1=new Student("12", "houssam",3.0);
s1.print();
}
}
1. Compile then execute the program from the cmd prompt.
2. Modify the program to prompt the user to input the information about a new object
s2, using JOptionpane methods. The output the information about s2 on the screen.
3. add set and get functions for each private member in the class definition.
4. Use the set and get functions to store in s1 the same values that are stored in s2.
3
Problem 3:
Write a java application to prompt the user to input two positive integers A,B (A<B).
Then the user should choose from the following menu: (use functions)
1. Output all prime numbers between A and B.
2. Count the even numbers between A and B.
3. Output the common divisors of A and B.
4. Exit.
Problem 4:
Write the class definitions and implementations for the following class specifications..
Address
Attributes
city: a string of characters.
area: a string of characters.
Methods
constructor
Set and get methods.
Print methods.
Write a driver program to create the following addresses:
“Beirut Hamra”
“Beirut Ashrafieh”
“Saida Abra”
“Saida Hilalieh”
Then the program should do the following:
Output all the data about the 3 addresses.
change all the addresses in Beirut to be in the “zalka”area.
Display the area of addresses in saida.
4