Temperature Calculator SWEN 131 1 import ecs 100

  • Slides: 6
Download presentation
Temperature. Calculator SWEN 131 1 import ecs 100. *; /** Program for converting between

Temperature. Calculator SWEN 131 1 import ecs 100. *; /** Program for converting between temperature scales */ public class Temperature. Calculator{ /** Constructor: Set up interface */ public Temperature. Calculator (){ UI. initialise(); UI. add. Button("F->C", this : : fahrenheit. To. Celsius); UI. add. Button("Formula", this : : print. Formula); } /** Convert from Fahrenheit to Celsius */ public void fahrenheit. To. Celsius(){ double fahren = UI. ask. Double("Fahrenheit: "); double celsius = (fahren - 32) * 5 / 9; UI. println(fahren + “F is " + celsius + " C"); } /** Print conversion formula */ public void print. Formula ( ) { UI. println("Celsius = (Fahrenheit - 32) *5/9"); } } © Peter Andreae, Karsten Lundqvist

Structure of the program SWEN 131 2 • Import statements • Comment • Class

Structure of the program SWEN 131 2 • Import statements • Comment • Class definition (defining the objects) • Class header • Constructor definition (defining what to do when making a new object) • Method definitions (defining Actions the objects can do) • Method header • Instructions for performing the action • Telling an object to perform some action • Putting a value into a place © Peter Andreae, Karsten Lundqvist

Elements of the program SWEN 131 3 • Comments vs Code • Keywords vs

Elements of the program SWEN 131 3 • Comments vs Code • Keywords vs Identifiers vs Strings vs numbers vs operators and punctuation • Keywords : words with special meaning in the Java Language eg: public, class, if, while, … mostly to do with the structure of the program • Identifiers : other words, used to refer to things in the program. mostly made up by the programmer, some are predefined. • Strings : bits of text that the program will manipulate. always surrounded by " and " • numbers • operators and punctuation : + - * / =. ; , ( ) { } [ ] ' " all have precise meanings and rules for use © Peter Andreae, Karsten Lundqvist

Writing your own programs SWEN 131 4 How? • Use other programs as models,

Writing your own programs SWEN 131 4 How? • Use other programs as models, and guess how to modify • Very useful strategy • Limiting • Hard to work out what's wrong if you don’t understand it © Peter Andreae, Karsten Lundqvist

A new program SWEN 131 5 • Calculator to convert inches to centimeters import

A new program SWEN 131 5 • Calculator to convert inches to centimeters import ecs 100. *; /** Program to convert inches to centimeters */ public class Temperature. Calculator{ …. . /** Convert from fahrenheit to celsius */ public void fahrenheit. To. Celsius(){ double fahren = UI. ask. Double("Farenheit: "); double celsius = (fahren - 32) * 5 / 9; UI. println(fahren + “F is " + celsius + " C"); } © Peter Andreae, Karsten Lundqvist

Writing your own programs SWEN 131 6 How? • Use other programs as models,

Writing your own programs SWEN 131 6 How? • Use other programs as models, and guess how to modify • Very useful strategy • Limiting • Hard to work out what's wrong if you don’t understand it • Understand the language ⇒ vocabulary ⇒ syntax rules ⇒ meaning (“semantics”) • Unlimited • Able to understand reason about your program © Peter Andreae, Karsten Lundqvist