Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

NUST COLLEGE OF ELECTRICAL & MECHANICAL

ENGINEERING

LAB # 03: Introduction to Java

Name:____________________ Reg #: ___________________

Tool used: Eclipse 2022


Objectives:
The main objective of this lab is to practice the basic programming concepts learnt so far e.g. variables,
their types how they assigned, passing command line arguments etc in JAVA. Use naming conventions.

Description:
https://www.eclipse.org/downloads/

Project Creation Steps:


 Adding Class to Project:
In Java, every application begins with a class name, and that class must match the filename.
Every line of code that runs in Java must be inside a class. A class should always start with an
uppercase first letter. Java is case-sensitive: "MyClass" and "myclass" has different meaning.
General Syntax:

Example:

Assuming your program contains no errors, the compiler generates a byte code program that is equivalent
of your source code. The compiler stores the byte code program in a file with the same name as source file,
but with the extension .class. Java executable modules are always stored in a file with the extension .class.
When you execute a java program, a program called the Java interpreter inspects the byte code for it, checks
it out to ensure that it has not been tampered and is safe to execute and then execute the actions that the
byte codes specify within the java virtual machine.
 static means that the method belongs to the Main class and not an object of the Main class.
 System.out.print() command in java is same as cout in C++
 System.out.println() command: System is a built-in Java class that contains useful members,
such as out, which is short for "output". The println() method, short for "print line", is used to
print a value to the screen (or a file). Note that it will add a new line for each method.

Java Variables:
 If you don't want others (or yourself) to overwrite existing values, use the final keyword
(this will declare the variable as "final" or "constant", which means unchangeable and read-
only):
final int myNum = 15;

Classes, Objects, Methods:


A method is a block of code or collection of statements or a set of code grouped together to
perform a certain task or operation. Method name must start with a lowercase letter.

1. public class InstanceMethodExample Output


2. { The sum is: 25
3. public static void main(String [] args)
4. {
5. //Creating an object of the class
6. InstanceMethodExample obj = new InstanceMethodExample();
7. //invoking instance method
8. System.out.println("The sum is: "+obj.add(12, 13));
9. }
int s;
//user-
defined method because we have not used static keyword
public int add(int a, int b)
{
s = a+b;
//returning the sum
return s;
}
}
Default constructor:

1. //Java Program to create and call a default constructor Bike is created


2. class Bike1{
3. //creating a default constructor
4. Bike1(){System.out.println("Bike is created");}
5. //main method
6. public static void main(String args[]){
7. //calling a default constructor
8. Bike1 b=new Bike1();
9. }
}

Overload the constructors:

1. class Student5{ 111 Karan 0


2. int id; 222 Aryan 25
3. String name;
4. int age;
5. //creating two arg constructor
6. Student5(int i,String n){
7. id = i;
8. name = n;
9. }
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){


Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
LAB TASKS

1. Create a class POINT to represent a point in Cartesian coordinate system Choose appropriate data
members. Provide member functions:
 Default constructor
 Parameterized constructor
 Input
 Output
 Distance( returns the distance between two POINT objects, the
function takes one object as an input)
 isZero(determines if a point is the center)
 Midlepoint(returns the middle point , takes one object as an input
and returns the answer in a POINT object)
 isEqualTo (compare two POINTs)
 isGreaterThan (compares two POINT in terms of the distance from the center )
o Above two function takes one POINT object as an argument and returns true if the
condition is satisfied and false otherwise

TEST PLAN

Commands Output
Point g;
g.setx(-2);
g.isZero();
g.gety();
g.MiddlePoint();
g.isEqualTo();
Point r(9,8.5);
r.isGeaterThan(g);
Point j=g;
g.isEqualTo(r);
g.isEqualTo(j);
Point f(-6.7,-9.5);
f.setx(5.5)
f.Distance(r);
g.Distance(f);
g.Distance(r);

2. Create a class SavingAccount that helps to maintain the accounts of different customers each
customer having its own savingsBalance and also stores an annualInterestRate. Provide a
member function to calculate Monthlyinterest that calculate the monthly interest by
(balance*annualInterestRate)/12. This interest should be added to savingBalance.
Use default and parameterized constructors.
*no use of arrays in this task*
TEST PLAN

Commands Output
SavingAccount y;
y.getBalance();
y.getMonthlyInterest();
SavingAccount v(90);
v=y;
v.getMonthlyInterest();
SavingAccount j=v;
j.getBalance();
j.getMonthlyInterest();
v.getBalance();
v.getMonthlyInterest();

3. Create a class called Rational for performing arithmetic with fractions. Write a program to test
your class. Use integer variables to represent the private data of the class— the numerator and
denominator. Provide public member functions that perform each of the following tasks:

• Adding two Rational numbers. The result should be stored in reduced form.
• Multiplying two Rational numbers. The result should be stored in reduced form.
• Dividing two Rational numbers. The result should be stored in reduced form.
• Printing Rational numbers in the form a/b, where a is the numerator and b is the
denominator.
• Printing Rational numbers in floating-point format.
• Default constructor and Parameterized constructor

Test Plan:
Commands Output
Rational a;
Rational b;
a.setvalue(7,8);
b.setvalue(6,5);
a.add(b);
Rational c;
c.setvalue(-5,7);
c.multiply(b);
c=a;
c.print();
Rational j=b;
a=b.divide(c);
Rational d;
d.setvalue(-5,0);
c.multiply(d);

You might also like