Personal Blog : My OUM(Open University Malaysia) journey starting from Jan 2011 till graduate.
Everyone can copy the contents in this blog and please leave some credit or a backlink to me... -Thank You-

Friday, April 6, 2012

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING

QUESTION 2

Implement a class named threeDPoint that models a location in (x,y,z) coordinate space. The class contains:

A double data field named X that specifies x coordinate in a three dimensional Cartesian coordinate system

A double data field named Y that specifies y coordinate in a three dimensional Cartesian coordinate system

A double data field named Z that specifies z coordinate in a three dimensional Cartesian coordinate system

A no-arg constructor that creates a default point 0.0 for all the data fields

A constructor that creates an instance with the specified x-, y-, and z-coordinates.

The accessor (get) and mutator (set) methods for all three data fields.

A method named toString () that returns a string description for the point.

Then, create an object of the above class in a new program that has the main() method. All the inputs for the object must be captured in the main() method interactively.

=========================ANSWER============================

/*
* Question 2
* JavaApplication2.
*/
package threeDPoint;
import java.util.Scanner;
/**
* @author AzAH
*/
public class JavaApplication2 extends ThreeDPoint
{
public static void main(String[] args)
{
//Code application logic
ThreeDPoint obj = new ThreeDPoint();
Scanner in = new Scanner(System.in);
System.out.println("Please insert x value:");
String x = (String) in.nextLine();
System.out.println("Please insert y value:");
String y = (String) in.nextLine();
System.out.println("Please insert z value:");
String z = (String) in.nextLine();
try{
if(! x.isEmpty())
{
obj.setX(Double.parseDouble(x));
}
} catch(Exception err){}
try{
if(! y.isEmpty())
{
obj.setY(Double.parseDouble(y));
}
} catch(Exception err){}
try{
if(! z.isEmpty())
{
obj.setZ(Double.parseDouble(z));
}
} catch(Exception err){}
String description = obj.toString();
System.out.println(description);
}
}


======================================================

/*
* Question 2
* ThreeDPoint
*/
package threeDPoint;
/**
* @author AzAH
*/
public class ThreeDPoint
{
// data field
private double X;
private double Y;
private double Z;
/**
* @param args the command line arguments
*/
public ThreeDPoint()
{
this.X = 0.00;
this.Y = 0.00;
this.Z = 0.00;
}
public ThreeDPoint(double x, double y, double z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public double getX()
{
return this.X;
}
public void setX(double X)
{
this.X = X;
}
public double getY()
{
return this.Y;
}
public void setY(double Y)
{
this.Y = Y;
}
public double getZ()
{
return this.Z;
}
public void setZ(double Z)
{
this.Z = Z;
}
public String toString()
{
return "Point is (" + this.X + ", " + this.Y + ", " + this.Z + ")";
}
}


No comments:

Post a Comment

Table Grade