CSC1109_Tutorials/Week2_Lab/Submission/CSC1109_Lab2_2200624.tex

273 lines
6.7 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 2}
\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 2}
\author{Woon Jun Wei, 2200624}
\date{\today}
\maketitle
\section*{Question 1}
Develop a program to accept the radius of one circle from the console and output
the area of the circle. Note that to compute the area, the following formula can be used: Area = radius * radius * PI
Define PI as a constant variable with a fixed value 3.14159
\begin{lstlisting}[label={list:first},caption=Question 1 Source Code.]
import java.util.*;
public class q1 {
static final double PI = 3.14159;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number for radius: ");
double radius = sc.nextDouble();
double area = PI * radius * radius;
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:first_output},caption=Question 1 Output.]
Enter a number for radius: 2.5
The area for the circle of radius 2.5 is 19.6349375
Enter a number for radius: 23
The area for the circle of radius 23.0 is 1661.90111
Enter a number for radius: 7.3
The area for the circle of radius 7.3 is 167.41533109999997
\end{lstlisting}
\pagebreak
\section*{Question 2}
Average calculation for multiple input from keyboard: In this question, you will design a program to accept three number inputs by the user, and display the average of it.
\begin{lstlisting}[label={list:second},caption=Question 2 Source code.]
import java.util.*;
public class q1 {
static final double PI = 3.14159;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number for radius: ");
double radius = sc.nextDouble();
double area = PI * radius * radius;
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:second_output},caption=Question 2 Output.]
Enter three numbers:10.5
11
11.5
The average of 10.5 11.0 11.5 is 11.0
Enter three numbers:1 2 3
The average of 1.0 2.0 3.0 is 2.0
Enter three numbers:10
11.5 19
The average of 10.0 11.5 19.0 is 13.5
\end{lstlisting}
\pagebreak
\section*{Question 3}
\begin{lstlisting}[label={list:third},caption=Question 3 Source Code.]
public class q3 {
public static void main(String[] args) {
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds/1000;
long currSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currrHour = totalHours % 24;
System.out.println("Current time is " + currrHour + ":" + currMinute + ":" + currSecond + " GMT");
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:third_output},caption=Question 3 Output.]
Current time is 6:49:27 GMT
\end{lstlisting}
\pagebreak
\section*{Question 4}
Chinese Zodiac Calculation: Now let us write a program to find out the Chinese Zodiac sign for
a given year. The Chinese Zodiac is based on a twelve-year cycle, with each year represented
by an animal- monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep—
in this cycle, as shown in Figure 2.
\\
Note that year \% 12 determines the Zodiac sign. 1900 is the year of the rat because 1900 \%
12 is 4. Listing 3.9 gives a program that prompts the user to enter a year and displays the
\begin{lstlisting}[label={list:fourth},caption=Question 4 Source Code.]
import java.util.*;
public class q4 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
switch(year % 12){
case 0:
System.out.println("monkey");
break;
case 1:
System.out.println("rooster");
break;
case 2:
System.out.println("dog");
break;
case 3:
System.out.println("pig");
break;
case 4:
System.out.println("rat");
break;
case 5:
System.out.println("ox");
break;
case 6:
System.out.println("tiger");
break;
case 7:
System.out.println("rabbit");
break;
case 8:
System.out.println("dragon");
break;
case 9:
System.out.println("snake");
break;
case 10:
System.out.println("horse");
break;
case 11:
System.out.println("sheep");
break;
default:
System.out.println("Invalid input");
break;
}
}
}
\end{lstlisting}
\begin{lstlisting}[label={list:fourth_output},caption=Question 4 Output.]
Enter a year: 1963
rabbit
Enter a year: 1877
ox
Enter a year: 2000
dragon
\end{lstlisting}
\end{document}