CSC1109_Tutorials/Week4_Lab/Submission/CSC1109_Lab4_2200624.tex

285 lines
7.5 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 4}
\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 4}
\author{Woon Jun Wei, 2200624}
\date{\today}
\maketitle
\section*{Question 1}
\textbf{Method assignments}
I first instantiated 3 variables: `heightInInches`, `weightInPounds` and `bmi`.
The constructor takes in 2 parameters, `heightInInches` and `weightInPounds`
and assigns them to the corresponding variables.\\
The getters and setters are required to access the variables from outside the class,
which was required for the `main` method in BMI\_Main.java.\\
\\
\textbf{calBMI()}
The `calBMI` method takes in 2 parameters, `heightInInches` and `weightInPounds`.
Using these variables, I first converted the \textbf{height} from inches to meters,
using the formula `heightInMeters = heightInInches * 0.0254` where 0.0254 is given as the conversion ratio in the lab handout and
I converted the \textbf{weight} from pounds to kilograms, using the formula `weightInKilograms = weightInPounds * 0.45359237` where 0.45359237 is given as the conversion ratio in the lab handout.
I then calculated the \textbf{BMI} using the formula `bmi = weightInKilograms / (heightInMeters * heightInMeters)`.\\
\\
\textbf{BMIInterpretation()}
The `BMIInterpretation` method takes in 1 parameter, `bmi` and returns a string
based on the value of `bmi`.\\
The method checks against values in the table provided in the lab sheet and returns the corresponding string.\\
\pagebreak
\begin{lstlisting}[label={list:BMI},caption=BMI.java]
public class BMI {
private double heightInInches;
private double weightInPounds;
private double bmi;
public BMI(double heightInInches, double weightInPounds){
this.heightInInches = heightInInches;
this.weightInPounds = weightInPounds;
}
public double getHeightInInches() {
return this.heightInInches;
}
public double getWeightInPounds() {
return this.weightInPounds;
}
public void setHeightInInches(double heightInInches) {
this.heightInInches = heightInInches;
}
public void setWeightInPounds(double weightInPounds) {
this.weightInPounds = weightInPounds;
}
public double calBMI(double heightInInches, double weightInPounds){
double heightInMeters = heightInInches * 0.0254;
double weightInKilograms = weightInPounds * 0.45359237;
this.bmi = weightInKilograms / (heightInMeters * heightInMeters);
return this.bmi;
}
public void BMIInterpretation() {
if (this.bmi < 18.5) {
System.out.println("Underweight");
} else if (this.bmi >= 18.5 && this.bmi < 25) {
System.out.println("Normal");
} else if (this.bmi >= 25 && this.bmi < 30) {
System.out.println("Overweight");
} else if (this.bmi >= 30) {
System.out.println("Obese");
}
}
}
\end{lstlisting}
\pagebreak
\textbf{BMI\_Main.java}
The `BMI\_Main` class uses the `BMI` class to calculate the BMI of a person. This is
the main function that was used to access an use the `BMI` class.\\
Using a Scanner, I first got the hight and weight from the user,
I then created a new `BMI` object and passed the height and weight to the constructor.
I called the `calBMI` method and passed the height and weight to the method.\\
Lastly, the `BMIInterpretation` method was called to print the BMI interpretation.
The input and output variables from the first output corresponds with the output from the lab sheet\\
\begin{lstlisting}[label={list:BMI_Main},caption=BMI\_Main.java]
import java.util.Scanner;
public class BMI_Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter your weight in pounds: ");
double weightInPounds = s.nextFloat();
System.out.print("Enter your height in inches: ");
double heightInInches = s.nextFloat();
BMI bmi = new BMI(heightInInches, weightInPounds);
System.out.println("BMI is " + bmi.calBMI(bmi.getHeightInInches(), bmi.getWeightInPounds()));
bmi.BMIInterpretation();
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:BMI_Output},caption=BMI\_Main.java Output]
//Output 1
Enter your weight in pounds: 146
Enter your height in inches: 70
BMI is 20.948603801493316
Normal
//Output 2
Enter your weight in pounds: 150
Enter your height in inches: 80
BMI is 16.478193272792794
Underweight
\end{lstlisting}
\pagebreak
\section*{Question 2}
\begin{lstlisting}[label={list:StackOfIntegers},caption=StackOfIntegers.java]
public class StackOfIntegers {
private int[] elements;
private int size;
public StackOfIntegers(){
this.elements = new int[16];
this.size = -1;
}
public StackOfIntegers(int capacity){
this.elements = new int[capacity];
this.size = -1;
}
public boolean empty(){
return size == -1;
}
public int peek(){
return elements[size - 1];
}
public void push(int value){
this.size++;
this.elements[size] = value;
}
public int pop(){
int value = elements[size];
this.size--;
return value;
}
public int getSize(){
return size;
}
}
\end{lstlisting}
\pagebreak
\begin{lstlisting}[label={list:TestStackOfIntegers},caption=TestStackOfIntegers.java]
public class TestStackOfIntegers {
public static void main(String[] args) {
StackOfIntegers stack = new StackOfIntegers();
for (int i = 0; i < 10; i++) {
stack.push(i);
}
while (!stack.empty()) {
System.out.print(stack.pop() + " ");
}
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:StackOfIntegers},caption=TestStackOfIntegers.java Output]
9 8 7 6 5 4 3 2 1 0
\end{lstlisting}
\end{document}