Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.

Slides:



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

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
©Xiaoying Gao, Peter Andreae What is Programming About COMP 102 #2 2013T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
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.
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.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Chapter Introduction to Computers and Programming 1.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
1 Integrated Development Environment Building Your First Project (A Step-By-Step Approach)
A First Program Using C#
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
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.
©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.
© Peter Andreae What is Programming About COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
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.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
© Peter Andreae What is Programming About COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
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.
© 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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
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.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Microsoft Access Illustrated
TRANSLATORS AND IDEs Key Revision Points.
Chapter 10 Programming Fundamentals with JavaScript
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 # T1

© Peter Andreae COMP 102 3: 2 Menu MazeMouse (from lecture 2) Designing a Java program Announcements: Can’t find a lab slot that works? Class rep: vote on Monday. Current candidates: Read their statements on the forum Lost property from labs: ID cards, USB sticks  school office

© Peter Andreae COMP 102 3: 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 Step 1: Specification: what is it supposed to do? Write a program that will let the user do two things: let user enter temperature in Fahrenheit, and print out in Celsius. print out the conversion formula Step 2: Design: For calculate action: Ask user for the value to be converted Calculate new value out of given value (F-32)*5/9 Print out the answer Two Actions ⇒ two buttons

© Peter Andreae COMP 102 3: 6 Designing the Java program Step 3: Editing Need to write this design in the Java language.  Need an object : a "temperature calculator" - all actions must be performed on some object  Need a class to describe the object  The class needs a name  The class needs to specify a constructor to set up the interface  The class needs to specify the three actions its objects can do  Define three methods  Give names to the methods  specify what the methods will do

© Peter Andreae COMP 102 3: 7 Writing the Java code import ecs100.*; /** Program for converting between temperature scales */ public class TemperatureCalculator{ /** Constructor: Set up interface */ public TemperatureCalculator (){ UI.initialise(); UI.addButton("F->C", this :: fahrenheitToCelsius); UI.addButton("Formula", this :: printFormula); } /** Convert from fahrenheit to centigrade */ public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; UI.println(fahren + " F -> " + celsius + " C"); } /** Print conversion formula */ public void printFormula ( ) { UI.println("Celsius = (Fahrenheit - 32) *5/9"); } }

© Peter Andreae COMP 102 3: 8 Elements of the program Program Structure: Import list the library files you will use (We always use ecs100). Class Top level component 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. Constructor Called when object is created Typically sets up the user interface (in one-class programs) Methods Main elements of a class Each method describes an action that objects of this class can perform

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

© Peter Andreae COMP 102 3: 11 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. Let’s do it… editing in BlueJ

© Peter Andreae COMP 102 3: 12 Compiling and Running Step 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 Step 5: Running and Testing Must run the program and test it on lots of different input. BlueJ makes it easy to run individual methods.

© Peter Andreae COMP 102 3: 13 Compiling and Running Step 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 Let’s do it…

© Peter Andreae COMP 102 3: 14 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.