Sei sulla pagina 1di 12

KING ABDULAZIZ UNIVERSITY

Faculty of Computing & Information Technology


Department of Computer Science

Lab Manual

CPCS204
Data Structures 1
1436/1435H

Lab - 1

Learning Procedure

1) Stage J (Journey inside-out the concept)


2) Stage a1 (apply the learned)
3) Stage v (verify the accuracy)
4) Stage a2 (assess your work)
Term I
2015 Lab-1: Java Programming Environment

Laboratory 1:
Statement Purpose:
This lab will give you an overview of Java programming environment.

Activity Outcomes:
This lab teaches you the following topics:
 Review of the main Java programming concepts.
 Review of defining and handling classes ingredients.
 Recall algorithm and pseudo-code approaches.

Instructor Note:
As pre-lab activity, review Ch1, from the book Data Structures with Java by
John R. Hubbard and also the relevant instructor’s slides.

Names I.D.

1. .……………..………………………………. ………………………………

2. ..…………………………………………….. ………………………………

3. .……………………………………………... ………………………………

4. .…………………………………………….. ..…………………………….

CPCS204 – The Lab Note Lab-1 1


Term I
2015 Lab-1: Java Programming Environment

1) Stage J (Journey)
Java programming environment
 Java programming language specification
o Syntax of Java programs
o Defines different constructs and their semantics
 Java byte code: Intermediate representation for Java programs
 Java compiler: Transform Java programs into Java byte code
 Java interpreter: Read programs written in Java byte code and execute
them
 Java virtual machine: Runtime system that provides various services to
running programs
 Java programming environment: Set of libraries that provide services
such as GUI, data structures, etc.

Phases of a Java program

Java Source to Bytecode

CPCS204 – The Lab Note Lab-1 2


Term I
2015 Lab-1: Java Programming Environment

Primitive Data Types


In Java, our programs are all built from just a few basic types of data:

Classes Review

CPCS204 – The Lab Note Lab-1 3


Term I
2015 Lab-1: Java Programming Environment

Access Modifiers in Java *


1. Class level access modifiers (java classes only)
Only two access modifiers is allowed, public and no modifier

 If a class is ‘public’, then it CAN be accessed from ANYWHERE.


 If a class has ‘no modifier’, then it CAN ONLY be accessed from ‘same
package’.

2. Member level access modifiers (java variables and java methods)


All the four public, private, protected and no modifier is allowed.

 public and no modifier – the same way as used in class level.


 private – members CAN ONLY access.
 protected – CAN be accessed from ‘same
package’ and a subclass existing in any package
can access.

For better understanding, member level access is formulated as a table:

Access Modifiers Same Class Same Package Subclass Other packages


public Y Y Y Y
protected Y Y Y N
no access modifier Y Y N N
private Y N N N
* Form: J. Kulandai (2008), jvavapapers.com

CPCS204 – The Lab Note Lab-1 4


Term I
2015 Lab-1: Java Programming Environment

2) Stage a1 (apply)
1. Different data type available in Java

2. Defining a Class and an Instance Object of That Class


Try the code blocks below (you can copy and paste the code) and compile
them to figure out the difference between two blocks (i.e. example1 and
example2). Report your observation in the lab report.
Example1:
//Defining a Class
public class GreeterA
{
public String sayHello()
{
String message = "Hello, University!";
return message;
}
}

//Defining object
public class SimpleJava
{
public static void main (String[] args)
{
GreeterA worldGreeter = new GreeterA();
System.out.println(worldGreeter.sayHello());
}
}

CPCS204 – The Lab Note Lab-1 5


Term I
2015 Lab-1: Java Programming Environment

Example2:
//Defining a Class
public class GreeterB
{
public void sayHello()
{
String message = "Hello, University!";
System.out.println(message);
}
}

//SimpleJavaB for GreeterB


public class SimpleJavaB
{
public static void main(String[] args)
{
GreeterB worldGreeter = new GreeterB();
worldGreeter.sayHello();
}
}

3. How to use constructors


Try the code block below (you can copy and paste the code) and compile it to
figure out how to define and use different constructor of the same class.
Report the main benefits of defining multiple constructor of the same class (or
types).
// How to use constructor
public class MyCircle
{
private int xcenter;
private int ycenter;
private int radius;
public MyCircle( ) // default constructor (initializer)
{ xcenter = 0;
ycenter = 0;
radius = 1;
}
public MyCircle(int x, int y, int
r)//constructor(initializer)
{ xcenter = x;
ycenter = y;
radius = r;
}
public double area( )

CPCS204 – The Lab Note Lab-1 6


Term I
2015 Lab-1: Java Programming Environment

{return (3.14*radius*radius);}
public double perimeter( )
{
return (2*3.14*radius);
}
public void changeTheRadius(int r)
{
radius = r ; // or this.radius r, explicit
parameter
}
public void output( ) // that called the method
{
System.out.println("center: " + xcenter + "," + ycenter +
" radius: " + radius);
}
}

public class MyCircleTest


{
public static void main(String[ ] args)
{
MyCircle circle1, circle2;
circle1 = new MyCircle(3, 4, 10);
// instantiation i.e. declaration of Mycircle
objects
circle2 = new MyCircle( );
circle1.output( );
circle2.output( );
System.out.println(circle1.area( ));
System.out.println(circle2.area( ));
System.out.println(circle1.perimeter( ));
System.out.println(circle2.perimeter( ));
circle1.changeTheRadius(5);
circle1.output( );
circle2.output( );
circle1. changeTheRadius(6);
circle1.output( );
circle2.output( );
}
}

CPCS204 – The Lab Note Lab-1 7


Term I
2015 Lab-1: Java Programming Environment

3) Stage v (verify)
Lab Activities:
Task 1:
Number the marked statements from 1 to 7, in the blanks at the right for the
following program fragment, to show the logical order in which they are
executed.

Hint:
Verify your answer by using a static variable as a counter, then use print
command with each statement to show the (counter value) numbering
result.

public class Problem2


{
public void alsoDo(int value1, float value2)
{
System.out.println(value1 + value2);
this.doThis(value1, value2); // ________
}
public void doThis(int num1, float num2)
{
int num3 = 3;
System.out.println(num1 + num2 + num3); // ________ ________
}
}
public class Problem2Tester
{
public static void main(String[ ] args)
{
System.out.println("Number "); // ________
int number1 = 1;
float number2 = 2.5f;
Problem2 object2 = new Problem2( );
object2.doThis(number1, number2); // ________
object2.alsoDo(number1, number2); // ________
System.out.println(number1 + " " + number2); // ________
}
}

CPCS204 – The Lab Note Lab-1 8


Term I
2015 Lab-1: Java Programming Environment

Task 2:
Based on the rules that you studied in the course lecture about expressing an
algorithm and a pseudo-code for a given problem.

1) Write program code that translates your solution for problems 1 and 2.

Problem 1:

Problem 2:

CPCS204 – The Lab Note Lab-1 9


Term I
2015 Lab-1: Java Programming Environment

Home Activities (extra):


Task 4:
Review and implement the java program of (EXAMPLE 1.6 Implementing
Associations) in the book chapter1, page 10-13.

Task 5:
Based on the rules that you studied in the course lecture about expressing an
algorithm and a pseudo-code for a given problem.

1) Write an algorithm to solve the following problem.


2) Write a corresponding pseudo-code of the same proposed algorithm.
3) Try to estimate the complexity time of your proposed algorithm.
4) Write a program code that translate your solution.

Problem:

For given 3 digits, apply the below rule to check that all possible permutations*
of a 3-digit integer number comprising the given 3 digits are divisible by 3 or
not.

* For example, for given three digits (5, 4, and 8), all possible 3-digit integer
number permutations are (548, 584, 458, 485, 845, and 854). Based on the
rule, they are all indivisible by 3.

Rule:
If the sum of three given digits is divisible by 3, so that any 3-digit number
comprising the given 3 digits is also divisible by 3.

CPCS204 – The Lab Note Lab-1 10


Term I
2015 Lab-1: Java Programming Environment

4) Stage a2 (assess)
Lab Work:
In each laboratory you are assessed on your work within lab session based on
your participation, discussions and achievement of lab activities. Thus, each lab
has a portion of the (LAB WORK MARK). Therefore, a checklist of each lab is
used to evaluate your work. This checklist accounts the following criteria:

 Following the lab manual step by step


 Answering given questions concisely and precisely
 Practicing and implementing given examples correctly
 Writing code of required programming tasks
 Being focused, positive, interactive and serious during lab session
 Asking good questions or answering instructor questions if any

Note: performing given home activities or extra programming is highly


recommended to improve your understanding, capability and programming
skills.

CPCS204 – The Lab Note Lab-1 11

Potrebbero piacerti anche