Skip to main content

My First Control Structure in Java

Here is my first original program, written after 5 days of learning java. The goal was to include a looping control structure with decision making paths (if/else).
____________________________________________________________________
import java.util.Scanner;

public class AverageTemp 
{
//Calculates the average temperature of weekdays in centigrade 

public static void main(String[] args) 
{
Scanner keyboard = new Scanner (System.in);

double monday, tuesday, wednesday,thursday,friday;
char option;

//User input for five weekdays
do
{
System.out.println ("What was the average temperature on Monday?:");
monday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Tuesday?:");
tuesday = keyboard.nextDouble();
 System.out.println ("What was the average temperature on Wednesday?:");
wednesday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Thursday?:");
thursday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Friday?:");
friday = keyboard.nextDouble();

double average = (monday + tuesday + wednesday + thursday + friday)/5;

System.out.println ("The average temperature this week was: " + average);

//Decision Tree comments on temperature

if (average >= 38)
{ System.out.println("Wow, that is hot!");
}

else if (average <= -10)
{ System.out.println ("Brrr!");
}

else 
{ System.out.println ("That's all right.");
}
//option to continue or end program

System.out.println ("Enter Y to continue or N to quit: ");
option = keyboard.next().charAt(0);

while (option == 'Y' || option == 'y');

}
}

Comments

Popular posts from this blog

CodeSchool vs Codecademy(or 'How socket inherits event listening methods and implements asynchronicity')

In this review I'm going to focus on the pedagogy that I see evident in some CodeSchool courses and compare them to  Codecademy. By pedagogy, I mean: 'How does CodeSchool teach?' and ' Does it do a good job of teaching?'. I'm going to argue that despite high quality videos, colourful web pages, and often ssspppeeeeeakkkiiiing...rrrreeally...slowly..., CodeSchool's pedagogy is inferior to that of Codecademy. There are many fantastic resources for learning to code on the web, and CodeSchool is one of them. So far I have completed courses in Ruby, Rails, Javascript, HTML/CSS, Jquery and Git on CodeSchool. The courses have all included high quality videos and colourful, interactive exercises- as well as  massive  pdf files of the slides ( the files take more than a minute to load on my machine .) The question is: does the higher production value mean better educational quality? The 'Try' courses on CodeSchool(such as Try Ruby and Try jQuery) are f...

Algorithm Analysis - 1

Currently, I'm learning to perform algorithm analysis using Big-O notation. In one resource I found the following problem: You just dropped a box of glass toys and  n  toys in the box broke in half. You'd like to match the halves of the toys so that you could glue them together, but the only way to tell whether two halves belonged to one toy is to physically pick up the two pieces and try to fit them together. Express how long this matching process will take in terms of  n . The answer given is n^2 (n squared) with the following explanation:  You have to compare every piece with every other piece. If you have 1 toy and it breaks in half, you have 1 comparison to make. If you have 2 toys and they both break in half there are 4 pieces and you have to do 6 comparisons. If you have 3 toys, there are 6 pieces and you have to do 15 comparisons. If you have  N/2  toys, you have  N  pieces and you have to do N-1 + N-2 + N-3 + ... + 1 =(N)(N-1)/2...

Venturing into Number Theory

In attempt to solve a maths problem with Ruby, I discovered the benefits of going beyond simple arithmetic. Perhaps surprisingly, many seemingly complex mathematical problems can be solved using basic arithmetic and raw computing power. Most of the time this doesn't pose a problem. However, as the values involved grow larger, the demands on the computer's memory reaches a critical point beyond which you risk crashing your computer.  The problem I was trying to solve was the following:    What is the smallest positive number that is evenly divisible  by all of the numbers from 1 to 20? After causing my computer to hang for several minutes, I realized I had to rewrite the program I was working on. The program had to take shortcuts, so that instead of testing every single value between 1 and 1 million, it only tested likely values. With the help of my lovely wife (a Maths teacher) I rewrote the program using prime numbers.   class SmallestProduct requ...