CSC1109_Tutorials/Week5_Lab/Submission/CSC1109_Lab5_2200624.tex

390 lines
9.4 KiB
TeX

\documentclass[a4paper,11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}
\renewcommand\familydefault{\sfdefault}
\usepackage{tgheros}
\usepackage[defaultmono]{droidsansmono}
\usepackage{amsmath,amssymb,amsthm,textcomp}
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{geometry}
\geometry{left=25mm,right=25mm,%
bindingoffset=0mm, top=20mm,bottom=20mm}
\linespread{1.3}
\newcommand{\linia}{\rule{\linewidth}{0.5pt}}
% custom theorems if needed
\newtheoremstyle{mytheor}
{1ex}{1ex}{\normalfont}{0pt}{\scshape}{.}{1ex}
{{\thmname{#1 }}{\thmnumber{#2}}{\thmnote{ (#3)}}}
\theoremstyle{mytheor}
\newtheorem{defi}{Definition}
% my own titles
\makeatletter
\renewcommand{\maketitle}{
\begin{center}
\vspace{2ex}
{\huge \textsc{\@title}}
\vspace{1ex}
\\
\linia\\
\@author \hfill \@date
\vspace{4ex}
\end{center}
}
\makeatother
%%%
% custom footers and headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{}
\lfoot{CSC 1109 Lab 5}
\cfoot{}
\rfoot{Page \thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%
% code listing settings
\usepackage{listings}
\lstset{
language=java,
basicstyle=\ttfamily\small,
aboveskip={1.0\baselineskip},
belowskip={1.0\baselineskip},
columns=fixed,
extendedchars=true,
breaklines=true,
tabsize=4,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color[rgb]{0.627,0.126,0.941},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{01,0,0},
numbers=left,
numberstyle=\small,
stepnumber=1,
numbersep=10pt,
captionpos=t,
escapeinside={\%*}{*)}
}
%%%----------%%%----------%%%----------%%%----------%%%
\begin{document}
\title{CSC 1109 Lab 5}
\author{Woon Jun Wei, 2200624}
\date{\today}
\maketitle
\section*{Question 1}
\begin{lstlisting}[label={list:GeometricObject},caption=GeometricObject.java]
public class GeometricObject {
private String color;
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject() {
this.color = "white";
this.filled = false;
this.dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled){
this.color = color;
this.filled = filled;
this.dateCreated = new java.util.Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString(){
return " created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:CircleFromSimpleGeometricObject},caption=CircleFromSimpleGeometricObject.java]
public class CircleFromSimpleGeometricObject extends GeometricObject{
private double radius;
public CircleFromSimpleGeometricObject(){
super();
}
public CircleFromSimpleGeometricObject(double radius){
super();
this.radius = radius;
}
public CircleFromSimpleGeometricObject(double radius, String color, boolean filled){
super();
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea(){
return Math.PI * Math.pow(this.radius, 2);
}
public double getPerimeter(){
return 2 * Math.PI * this.radius;
}
public double getDiameter(){
return 2 * this.radius;
}
public void printCircle(){
System.out.println("The color is " + getColor() + "\nThe area is " + getArea() + "\nthe diameter is " + getDiameter());
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:RectangleFromSimpleGeometricObject},caption=RectangleFromSimpleGeometricObject.java]
public class RectangleFromSimpleGeometricObject extends GeometricObject{
private double width;
private double height;
public RectangleFromSimpleGeometricObject(){
super();
}
public RectangleFromSimpleGeometricObject(double width, double height){
super();
this.width = width;
this.height = height;
}
public RectangleFromSimpleGeometricObject(double width, double height, String color, boolean filled){
super(color, filled);
this.width = width;
this.height = height;
}
public double getWidth() {
return this.width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight(){
return this.height;
}
public double setHeight(){
return this.height;
}
public double getArea(){
return this.width * this.height;
}
public double getPerimeter(){
return 2 * (this.width + this.height);
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:TestCircleRectangle},caption=TestCircleRectangle.java]
public class TestCircleRectangle {
public static void main(String[] args){
CircleFromSimpleGeometricObject circle = new CircleFromSimpleGeometricObject(1);
System.out.println("A circle " + circle.toString());
System.out.println("The color is " + circle.getColor());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());
RectangleFromSimpleGeometricObject rectangle = new RectangleFromSimpleGeometricObject(2, 4);
System.out.println("A rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " + rectangle.getPerimeter());
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:TestCircleRectangle},caption=TestCircleRectangle.java]
A circle created on Fri Jan 27 15:07:16 SGT 2023
color: white and filled: false
The color is white
The radius is 1.0
The area is 3.141592653589793
The diameter is 2.0
A rectangle created on Fri Jan 27 15:07:16 SGT 2023
color: white and filled: false
The area is 8.0
The perimeter is 12.0
\end{lstlisting}
\section*{Question 2}
\begin{lstlisting}[label={list:Shape},caption=Shape.java]
public abstract class Shape {
public float dim1;
public float dim2;
public float PI = 3.14f;
public Shape(float dim1, float dim2) {
this.dim1 = dim1;
this.dim2 = dim2;
}
public abstract float area();
}
\end{lstlisting}
\begin{lstlisting}[label={list:Circle},caption=Circle.java]
public class Circle extends Shape{
public Circle(float dim1, float dim2) {
super(dim1, dim2);
}
public float area() {
return dim1 * dim1 * PI;
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:Ellipse},caption=Ellipse.java]
public class Ellipse extends Shape{
public Ellipse(float dim1, float dim2) {
super(dim1, dim2);
}
public float area() {
return dim1 * dim2 * PI;
}
}
\end{lstlisting}
\pagebreak
\begin{lstlisting}[label={list:Rectangle},caption=Rectangle.java]
public class Rectangle extends Shape{
public Rectangle(float dim1, float dim2) {
super(dim1, dim2);
}
public float area() {
return dim1 * dim2;
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:Square},caption=Square.java]
public class Square extends Shape{
public Square(float dim1, float dim2) {
super(dim1, dim2);
}
public float area() {
return dim1 * dim2;
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:Triangle},caption=Triangle.java]
public class Triangle extends Shape{
public Triangle(float dim1, float dim2) {
super(dim1, dim2);
}
public float area() {
return 0.5f * dim1 * dim2 ;
}
}
\end{lstlisting}
\pagebreak
\begin{lstlisting}[label={list:ShapeTest},caption=ShapeTest.java]
public class ShapeTest {
public static void main(String[] args){
Rectangle r = new Rectangle(9,5);
Triangle t= new Triangle(10,8);
Circle c = new Circle(5,5);
Ellipse e = new Ellipse(7,7);
Square s = new Square(6,6);
Shape figref;
figref = r;
System.out.println("Inside Area for Rectangle.");
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Inside Area for Triangle.");
System.out.println("Area is " + figref.area());
figref = c;
System.out.println("Inside Area for Circle.");
System.out.println("Area is " + figref.area());
figref = e;
System.out.println("Inside Area for Ellipse.");
System.out.println("Area is " + figref.area());
figref = s;
System.out.println("Inside Area for Square.");
System.out.println("Area is " + figref.area());
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:ShapeTestOutput},caption=ShapeTest.java Output]
Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
Inside Area for Circle.
Area is 78.5
Inside Area for Ellipse.
Area is 153.86
Inside Area for Square.
Area is 36.0
\end{lstlisting}
\end{document}