Anda di halaman 1dari 23

Course Supervisor: Dr.

Humera Tariq
Hands on Lab Sessions: Ms. Sanya Yousuf
UML to represent and using single object
Practice writing code for class
Practice toString( ) function
Practice writing your own functions
Practice Debugging Feature
Practice building code logic

Lab and Bonus Work


Task I: Write Code for
RationalNumber.java so that given
main( ) function executes successfully.

Read instructions and slides for proper


Lab completion
Study UML diagram for using Rational Numbers
RationalNumberDemo RationalNumber

- Numerator: int
- Denominator : int
+ main (args : String[]) : void
+ RationalNumber( )

+ RationalNumber(n:int,d:int)

+ RationalNumber(RationalNumber r)

+ add (q: RationalNumber) : void


+ add(int a):RationalNumber
+ toString() : String

Recall what are


rational numbers
Open new Project and Recall Code Organization

Auto generated File


created by Compiler
and contains main ( )
function

File
RationalNumber.java
is explicitly created by
developer

We will test our object in


2 main( ) after coding class
RationalNumber.java
Write Code for RationalNumber.java. Open your
Lectures and Recall we will write following functions.
1- Constructors
Null Constructor
Parameterized Constructor
Copy Constructor

2- Setters and getter Functions


void setNumerator(int n);
void setDenominator(int d);
void setRationalNumber(int n, int d);
int getNumerator( );
int getDenominator( );

3- Our desired functions


- add two rational numbers
- add a rational number with Integer
- subtract two rational numbers
- subtract an Integer from rational number

4- Methods to print/show our object


- show( )
- toString ( ) 3 Constructors,3 Setters 2
getters and 2 show
methods
Kindly Put on your thinking cap and start coding in
class RationalNumber.java
Recall the syntax of and purpose of show( ) and
toString( ). They present object Information to user.

show( ) is user
defined
method

toString( ) is inherited
method from built-in toString( ) contains
java class Object class attributes
separated by + and

Put on your thinking cap again and start testing in
class RationalNumberDemo.java

Test/Run/call each
function of
RationalNumber.java
inside main.java

See Next Slide for


Sample main( )
function and its
execution
Modify class RationalNumber.java so that
following main ( ) executes successfully. Please
code line by line, resolve error then code further.
Different Denominator Same Denominator
3 5
= +6 3 5
4 = 4+4
36 + (45) 3+5
= =
46 4
18+20
= 24 =4
8
38
Recall your can only = 25
code If you know its
manual solution.

Can your demonstrate what will happen in


Memory when add function call Occurs. If yes
Draw it on your copy and show to teacher
during Lab or in upcoming Theory Class.
Make Habit of overriding toString() method in your class to print object
using sout.
Task 2: Practice Debugging feature in every
lab to resolve your errors on your own
Using Debug Feature: step into

1- Put mouse on gray area and click


2- Debug Project

3- step Into
Using Debug Feature: step over

4- step over

5- step over

6- step over

7- step over
Using Debug Feature: Reading Variables
inside class add( ) function
Using Debug Feature: Reading Variables
inside class main

Memory
Before
step over

8- step over
Memory
after
step over
Task I: Write Code for
RationalNumber.java so that given
main( ) function executes successfully.

Read instructions and slides for proper


Lab completion
Write function add (int a) in class RationalNumber.java and call it from
main( ) function so that it execute successfully

38 2
= +
24 1
381 + (224)
=
241
Is it function Overloading? (True/False) =
38+48
Explain why or why not ? 86
24
= 24
Write function equalTo (RationalNumber b) in class RationalNumber.java
and call it from main( ) function as follows so that it execute successfully
and gives accurate result.
Write function greaterThan (RationalNumber b) in class
RationalNumber.java and call it from main( ) function as follows so that
it execute successfully and gives accurate result.
1- What other important functions should be written in
RationalNumber.java so that it becomes the best for users.
Code any TWO such functions and discuss it in upcoming Theory Class.

2- Practice Coding class Point and class Line

3- write function fight( ) with proper paramters to class Wrestler of


Lab 02. How you handle different moves like signature and finisher
in your coding logic.

Anda mungkin juga menyukai