© Peter Andreae Java Programs COMP 102 #3 2011 T1 Peter Andreae Computer Science Victoria University of Wellington.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

An Introduction to Programming with C++ Fifth Edition Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Chapter 8 Improving the User Interface
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
© Peter Andreae What is Programming About COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Chapter 16 Programming and Languages: Telling the Computer What to Do.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Programming What is a program? –A set of instructions –Understood by a computer.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Software Engineering 1 (Chap. 1) Object-Centered Design.
C++ Crash Course Class 1 What is programming?. What’s this course about? Goal: Be able to design, write and run simple programs in C++ on a UNIX machine.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Activity 1 - WBs 5 mins Go online and spend a moment trying to find out the difference between: HIGH LEVEL programming languages and LOW LEVEL programming.
Computer Science 101 Introduction to Programming.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
6 Steps of the Programming Process
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
1 Integrated Development Environment Building Your First Project (A Step-By-Step Approach)
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
INTRODUCTION TO C PROGRAMMING LANGUAGE Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
General Programming Introduction to Computing Science and Programming I.
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Computer Science 101 Introduction to Programming.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
© Peter Andreae What is Programming About COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
How to Program? -- Part 1 Part 1: Problem Solving –Analyze a problem –Decide what steps need to be taken to solve it. –Take into consideration any special.
Programming Errors. Errors of different types Syntax errors – easiest to fix, found by compiler or interpreter Semantic errors – logic errors, found by.
TCU CoSc Introduction to Programming (with Java) Java Language Overview.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 1: Introduction to Computers and Programming.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Lecture 2 Introduction to Programming
Microsoft Access Illustrated
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Programming.
Presentation transcript:

© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington

© Peter Andreae COMP 102 3:2 Menu A Java program Announcements: Extra labs for people who couldn't log in. Class rep

© Peter Andreae COMP 102 2:3 Learning to Program in Java What’s involved? Understand what the computer can do and what the language can specify Problem solving: program design, data structuring, Programming language (Java): syntax and semantics style and common patterns libraries of code written by other people Testing and Debugging (fixing). Common patterns in program design. Important data structures and algorithms.

© Peter Andreae COMP 102 3:4 Constructing Programs Design Edit typing in the Java code Compile Translating to executable instructions Run Testing the program to see that it works Specification syntax errors logic errors

© Peter Andreae COMP 102 3:5 A first Java Program Task: Write a temperature conversion program: C ⇔ F 1: Specification: what is it supposed to do? Write a program that will let the user do three things: let user enter temperature in Fahrenheit, and print out in Celsius. let user enter temperature in Celsius, and print out in Fahrenheit print out the conversion formula 2: Design: For calculate actions: Ask user for the value to be converted Calculate new values out of old value (F-32)*5/9 or C*9/5+32 Print out the answer

© Peter Andreae COMP 102 3:6 Designing the Java program 3: Editing Need to write this design in the Java language. We need an object (a "temperature calculator") to do the actions ⇒ we need a class to describe the object The object needs three actions ⇒ define three methods ⇒ specify what the methods will do

© Peter Andreae COMP 102 3:7 BlueJ BlueJ is an IDE for Java (Integrated Development Environment) Class manager, for keeping track of the files in your program Editor for entering and modifying the program Built-in compiler interface to help compile and fix the syntax errors Special interface to make it easy to construct objects and call methods on them.

© Peter Andreae Writing the Java code import comp102.*; /** Program for converting between temperature scales */ public class TemperatureCalculator{ /** Convert from fahrenheit to centigrade */ public void fahrenToCelsius(){ double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; UI.println(" -> " + celsius + " C"); } /** Convert from centigrade to fahrenheit */ public void celsiusToFahren(){ double celsius = UI.askDouble("Celsius:"); double fahren = celsius * 9 / ; UI.println(" -> " + fahren + " F"); } /** Print conversion formula */ public void printFormula ( ) { UI.println("Celsius = (Fahrenheit - 32) *5/9"); } COMP 102 3:8

© Peter Andreae COMP 102 3:9 Elements of the program Program Structure: Import list the library files you will use. Class Top level components of program Describes a class of objects Specifies the set of actions this kind of object can perform (Can also specify information the objects can hold) Note name, and conventions for naming. Methods Main elements of a class Each method describes one action that the objects of this class can perform

© Peter Andreae COMP 102 3:10 Elements of the program 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 Actions in a program Method calls telling an object to do one of its methods, passing the necessary information: UI.println("Celsius = (Fahrenheit - 32) *5/9"); UI.drawRect(100, 200, 50, 75); What are the objects? what are the methods. UI object has methods for printing asking reading drawing …. Assignment statements putting a value in a place double fahren = celsius * 9 / ; double celsius = UI.askDouble("Celsius:"); COMP 102 3:11

© Peter Andreae Compiling and Running 4: Compiling If there are syntax errors (invalid Java) then the compiler will complain and list all the errors ⇒ read the error message to work out what's wrong ⇒ fixing syntax errors until it compiles without complaint BlueJ makes this process easier 5: Running and Testing Must run the program and test it on lots of different input. BlueJ makes it easy to run individual methods. COMP 102 3:12

© Peter Andreae COMP 102 3:13 Using BlueJ for Java Programs Simple use of BlueJ for simple programs: 1.Edit the class file(s) to define the methods 2.Compile the class 3.Create an object of the class right click on the rectangle representing the class select “new…..” ⇒ a red square representing the object 4.Call methods on the object right click on the square representing the object select the method.