Professional Documents
Culture Documents
Lab 3
Lab 3
ENGINEERING
Description:
https://www.eclipse.org/downloads/
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;
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);