In JAVA please Using as reference the design model below, write the java code fo
ID: 3732840 • Letter: I
Question
In JAVA please
Explanation / Answer
import java.util.*;
//Class Point2D definition.
class Point2D{
private
float x;
float y;
public
//Parametrised constructor
Point2D(float x,float y){
this.x = x;
this.y = y;
}
//Default constructor
Point2D(){
this.x=0.0f;
this.y=0.0f;
}
//getter function to get x
float getX(){
return this.x;
}
//setter function to set x
void setX(float x){
this.x = x;
}
//getter function to get y
float getY(){
return this.y;
}
//setter function to set y
void setY(float y){
this.y = y;
}
//getter function to get value of x and y
float[] getXY(){
float arr[] = new float[2];
arr[0]=this.x;
arr[1]=this.y;
return arr;
}
//setter function to set value of x and y
void setXY(float x,float y){
this.x = x;
this.y = y;
}
@Override
public String toString(){
String str1,str2;
str1 = Float.toString(getX());
str2 = Float.toString(getY());
return str1+" "+str2;
}
}
//Derived class definition
class Point3D extends Point2D{
private
float z;
public
//Parametrised constructor
Point3D(float x,float y,float z){
setXY(x,y);
this.z = z;
}
//Default constructor
Point3D(){
setXY(0.0f,0.0f);
this.z = 0.0f;
}
//getter function to get value of z
float getZ(){
return this.z;
}
//setter function to set value of z
void setZ(float z){
this.z = z;
}
//getter function to get value of x,y and z
float[] getXYZ(){
float arr[] = new float[3];
arr[0]=getX();
arr[1]=getY();
arr[2]=this.z;
return arr;
}
//setter function to get value of x,y and z
void setXYZ(float x,float y,float z){
setXY(x,y);
this.z = z;
}
@Override
public String toString(){
String str1,str2,str3;
str1 = Float.toString(getX());
str2 = Float.toString(getY());
str3 = Float.toString(getZ());
return str1+" "+str2+" "+str3;
}
}
//Tester class
public class Main
{
public static void main(String[] args) {
Point2D p2 = new Point2D(2.0f,3.5f);
Point3D p3 = new Point3D(-1.0f,2.9f,4.3f);
System.out.println("x = "+p2.getX()+" y = "+p2.getY());
System.out.println("x = "+p3.getX()+" y = "+p3.getY()+" z = "+p3.getZ());
p2.setXY(20.0f,30.5f);
p3.setXYZ(-10.0f,20.9f,40.3f);
System.out.println(p2.toString());
System.out.println(p3.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.