Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Introduction To Object Oriented Systems Development

Uploaded by

Sam Mumo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Introduction To Object Oriented Systems Development

Uploaded by

Sam Mumo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Running Head: INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT.

Introduction to Object Oriented Systems Development

Name of Student

Name of Institution
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 2

Introduction to Object Oriented Systems Development


Task 1: interface: Shape
public interface Shape {
public double Pi = 3.14;

void UserName(String s);


void shapeName(String s);
void summaryPrint();
}

Task 2: abstract Class: TwoDimensionShape

public abstract class TwoDimensionShape implements Shape{


private String uName;
private String sName;
public abstract float findArea();
public abstract float findPerimeter();
public void UserName(String s){
uName=s;
}
public void ShapeName(String s){
sName=s;
}

Task 2.1: Class: Square


import java.util.Scanner;
public class Square extends TwoDimensionShape{
private float sideLength;
public Square(){
setSideLength();
summaryPrint();
}
public Square(float perimeter){
sideLength = findSquareSideLength(perimeter);
summaryPrint();
}
public void setSideLength(){
while(true){
try{
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 3

Scanner myObj = new Scanner(System.in); // Create a Scanner


object
System.out.println("Enter sideLength of square: ");
sideLength = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}
public float getSidelength(){
return sideLength;
}
public float findArea(){
return sideLength*sideLength;
}
public float findPerimeter(){
return 4f*sideLength;
}
public float findSquareSideLength(float perimeter){
return(perimeter/4f);
}
public void summaryPrint(){
System.out.println("shape name: Square");
System.out.println("side length: "+String.valueOf(getSidelength()));
System.out.println("Area: "+String.valueOf(findArea()));
System.out.println("Perimeter: "+ String.valueOf(findPerimeter()));
}

Task 2.2: Class: Circle

import java.util.Scanner;
public class Circle extends TwoDimensionShape{
private float radius;
public Circle(){
ShapeName("Circle");
setRadius();
summaryPrint();
}
public Circle(float perimeter){
radius = findRadius(perimeter);
summaryPrint();
}
public void setRadius(){
while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter radius of Circle: ");
radius = Float.parseFloat(myObj.nextLine());
break;
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 4

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}
public float getRadius(){
return radius;
}
public float findArea(){
return pi*radius*radius;
}
public float findPerimeter(){
return 2f*pi*radius;
}
public float findRadius(float perimeter){
return(perimeter/(2f*pi));
}
public void summaryPrint(){
System.out.println("Shape name: Circle");
System.out.println("Radius: "+String.valueOf(getRadius()));
System.out.println("Area: "+String.valueOf(findArea()));
System.out.println("Perimeter: "+ String.valueOf(findPerimeter()));
}

Task 2.3: Class: Triangle


import java.util.Scanner;
public class Triangle extends TwoDimensionShape{
private float sideLength;
public Triangle(){
setSideLength();
summaryPrint();
}
public Triangle(float perimeter){
sideLength = findTriangleSideLength(perimeter);
summaryPrint();
}
public void setSideLength(){
while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter sideLength of Triangle: ");
sideLength = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}
public float getSidelength(){
return sideLength;
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 5

}
public float findArea(){
return ((float)Math.sqrt(3)/4f)*sideLength*sideLength;
}
public float findPerimeter(){
return 3f*sideLength;
}
public float findTriangleSideLength(float perimeter){
return(perimeter/3f);
}
public void summaryPrint(){
System.out.println("shape name: Triangle");
System.out.println("side length: "+String.valueOf(getSidelength()));
System.out.println("Area: "+String.valueOf(findArea()));
System.out.println("Perimeter: "+ String.valueOf(findPerimeter()));
}

Task 3: abstract Class: ThreeDimensionShape


public abstract class ThreeDimensionShape implements Shape{
private String uName;
private String sName;
public abstract float calculateSurfaceArea();
public abstract float calculateVolume();
public void UserName(String s){
uName=s;
}
public void ShapeName(String s){
sName=s;
}

Task 3.1: Class: Cube


import java.util.Scanner;
public class Cube extends ThreeDimensionShape{
private float sideLength;
public Cube(){
setSideLength();
summaryPrint();
}
public Cube(float surfaceArea){
sideLength = findCubeSideLength(surfaceArea);
summaryPrint();
}
public void setSideLength(){
while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter sideLength of Cube: ");
sideLength = Float.parseFloat(myObj.nextLine());
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 6

break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}
public float getSidelength(){
return sideLength;
}
public float calculateSurfaceArea(){
return 6f*(sideLength*sideLength);
}
public float calculateVolume(){
return sideLength*sideLength*sideLength;
}
public float findCubeSideLength(float surfaceArea){
return((float)Math.sqrt(surfaceArea)/6f);
}
public void summaryPrint(){
System.out.println("shape name: Cube");
System.out.println("side length: "+String.valueOf(getSidelength()));
System.out.println("surfaceAreaArea:
"+String.valueOf(calculateSurfaceArea()));
System.out.println("Volume: "+ String.valueOf(calculateVolume()));
}

Task 3.2: Class: Cylinder


import java.util.Scanner;
public class Cylinder extends ThreeDimensionShape{
private float height;
private float radius;
public Cylinder(){
setHeight();
setRadius();
summaryPrint();
}
public Cylinder(float h,float r){
height = h;
radius = r;
summaryPrint();
}
public void setHeight(){
while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter height of Cylinder: ");
height = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 7

}
}
}
public void setRadius(){
while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter radius of Cylinder: ");
radius = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}

public float calculateSurfaceArea(){


return 2f*pi*(radius+radius*height);
}
public float calculateVolume(){
return pi*radius*radius*height;
}

public void summaryPrint(){


System.out.println("shape name: Cylinder");
System.out.println("height: "+String.valueOf(height));
System.out.println("radius: "+String.valueOf(radius));
System.out.println("surfaceAreaArea:
"+String.valueOf(calculateSurfaceArea()));
System.out.println("Volume: "+ String.valueOf(calculateVolume()));
}

Task 3.3: Class: Pyramid


import java.util.Scanner;
public class Pyramid extends ThreeDimensionShape{
private float height;
private float length;
private float side;
public Pyramid(){
setHeight();
setLength();
setSide();
summaryPrint();
}
public Pyramid(float h,float l, float s){
height = h;
length = l;
side = s;
summaryPrint();
}
public void setHeight(){
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 8

while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter height of Pyramid: ");
height = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}
public void setLength(){
while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter Length of Pyramid: ");
length = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}

public void setSide(){


while(true){
try{
Scanner myObj = new Scanner(System.in); // Create a Scanner
object
System.out.println("Enter Side of Pyramid: ");
side = Float.parseFloat(myObj.nextLine());
break;

}catch(Exception e){
System.out.println("invalid value enter again:");
}
}
}

public float calculateSurfaceArea(){


return 2f*length*(length+side);
}
public float calculateVolume(){
return length*length*height/3f;
}

public void summaryPrint(){


System.out.println("shape name: Pyramid");
System.out.println("height: "+String.valueOf(height));
System.out.println("length: "+String.valueOf(length));
System.out.println("side: "+String.valueOf(side));
System.out.println("surfaceAreaArea:
"+String.valueOf(calculateSurfaceArea()));
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 9

System.out.println("Volume: "+ String.valueOf(calculateVolume()));


}

Task 4: Public Class GeometricShapes


public class GeometricShapes
{
public static void main(String[] args) {
try{
boolean exit = false;
while(true){
int choice;
System.out.println("\t 1. 2D shape\n\t 2. 3D shape\n\t
3. Exit\n\t Enter your choice: ");
Scanner sc = new Scanner(System.in);
choice = Integer.parseInt(sc.nextLine());
switch(choice){
case 1:
TwoDimensionShape twoDShape;
System.out.println("1. Square \n2. Circle\n3.
Triangle\nEnter your choice: ");
int shapeType = Integer.parseInt(sc.nextLine());
System.out.println("1. Find Area and perimeter \n2.
find radius\\side length\nEnter your choice: ");
int finalChoice = Integer.parseInt(sc.nextLine());
switch(finalChoice){
case 1:
if(shapeType == 1){
twoDShape = new Square();
}
else if(shapeType ==2){
twoDShape = new Circle();
}
else if(shapeType ==3){
twoDShape = new Triangle();
}
break;
case 2:
System.out.println("Enter the perimeter:");
float perimeter =
Float.parseFloat(sc.nextLine());
if(shapeType == 1){
twoDShape = new Square(perimeter);
}else if(shapeType ==2){
twoDShape = new Circle(perimeter);
}
else if(shapeType ==3){
twoDShape = new Triangle(perimeter);
}
break;
default:
System.out.println("Wrong choice. going
back.");
break;
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 10

}
case 2:
ThreeDimensionShape threeDShape;
System.out.println("1. Cube \n2. Cylinder\n3. Pyramid\
n4. Exit to main menu\nEnter your choice: ");
int shapeType3d = Integer.parseInt(sc.nextLine());
switch(shapeType3d){
case 1:
System.out.println("1. Find SurfaceArea and
volume \n2. find radius\\side length\nEnter your choice: ");
int finalChoice3d =
Integer.parseInt(sc.nextLine());
if(finalChoice3d ==1){
threeDShape = new Cube();
}else if(finalChoice3d ==2){
System.out.println("Enter the surface
area:");
float SurfaceArea =
Float.parseFloat(sc.nextLine());
threeDShape = new Cube();
}else{
System.out.println("invalid input");
}
break;
case 2:
threeDShape = new Cylinder();
break;
case 3:
threeDShape = new Pyramid();
break;
}
break;
case 3:
exit = true;
break;
default:
System.out.println("invalid input");

}
if(exit){
break;
}
}
}catch(Exception e){
System.out.println("Invalid choice.Enter a correct choice!");
}

}
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 11

Task 5: UML

UML Diagram
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 12

Task 6: Testing
Test case Purpose Expected Result
test fixture Validate the behavior The methods are
of methods expected to behave
correctly.
unit test Run a certain They assert the
proper working of
functionality in the code.
objects.
integration test test the behavior of a The system is
expected to work as
component
intended by the
developer.
Performance tests Ensure the code test Testing does not
runs really fast. affect the efficiency
of the project.

Task 7: Individual
public class Circle {

private final double PI = 3.14159;


private double radius;

public Circle() {
radius = 0.0;
}

public Circle(double r) {
radius = r;
}

public void setRadius(double r) {


//setting r value
radius = r;
}

public double getRadius() {


//getter function for radius
return radius;
}

public double getArea() {


//function for calculating area of the circle
return PI * radius * radius;
}

public double getDiameter() {


// function for calculating diameter
INTRODUCTION TO OBJECT ORIENTED SYSTEMS DEVELOPMENT 13

return radius * 2;
}

public double getCircumference() {


//function for calculating circumference of the circle
return 2 * PI * radius;
}
}

public static void main(String[] args) {

// Create a Scanner object for keyboard input.


Scanner keyboard = new Scanner(System.in);

// Ask the user to input circle radius


System.out.print("Enter the radius of a circle: ");
double radius = keyboard.nextDouble();

// close keyboard
keyboard.close();

// Create a Circle object passing in user input


CircleClass circleClass = new CircleClass();
Circle circle = circleClass.new Circle(radius);

// Display circle information


System.out.println("Area is " + circle.getArea());
System.out.println("Diameter is " + circle.getDiameter());
System.out.println("Circumference is " + circle.getCircumference());

You might also like