Software and Programming 1

Similar documents
Software and Programming 1

Software and Programming 1

Software and Programming 1

Introduction Basic elements of Java

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 11 java track: lecture 1

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

( &% class MyClass { }

Object-Oriented Programming

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

COMP-202: Foundations of Programming

Introduction to Java Applications

Branching and Boolean Expressions

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

SOFT 161. Class Meeting 1.6

Programming with Java

Key Differences Between Python and Java

Loops and Expression Types

13 th Windsor Regional Secondary School Computer Programming Competition

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Java Bytecode (binary file)

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Lecture 2 Tao Wang 1

IT 374 C# and Applications/ IT695 C# Data Structures

Assignment Marking Criteria

Computational Expression

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Branching and Boolean Expressions

St. Edmund Preparatory High School Brooklyn, NY

Section 2.2 Your First Program in Java: Printing a Line of Text

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Computer Components. Software{ User Programs. Operating System. Hardware

4 Programming Fundamentals. Introduction to Programming 1 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Praktische Softwaretechnologie

Getting started with Java

CT 229 Fundamentals of Java Syntax

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Full file at

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Object-oriented programming in...

Lab # 2. For today s lab:

1.00/1.001 Tutorial 1

Pace University. Fundamental Concepts of CS121 1

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Fall 2017 CISC124 9/16/2017

Section 2.2 Your First Program in Java: Printing a Line of Text

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Basics of Java Programming

Full file at

Introduction to Software Development (ISD) David Weston and Igor Razgon

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

CS11 Java. Fall Lecture 1

An overview of Java, Data types and variables

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Java+- Language Reference Manual

Course Outline. Introduction to java

Building Java Programs. Introduction to Programming and Simple Java Programs

CS 231 Data Structures and Algorithms, Fall 2016

Introduction to Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Programming Basics. Digital Urban Visualization. People as Flows. ia

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Programming with Java

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

CS 106 Introduction to Computer Science I

Mr. Monroe s Guide to Mastering Java Syntax

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 2

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS260 Intro to Java & Android 03.Java Language Basics

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Elementary Programming

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Advanced Computer Programming

Getting started with C++ (Part 2)

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Imperative and Object Oriented Programming. Tutorial 1. Charlie Abela Department of Artificial Intelligence

DM550 Introduction to Programming part 2. Jan Baumbach.

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Introduction to Java & Fundamental Data Types

CS 177 Recitation. Week 1 Intro to Java

COMP 202 Java in one week

Lecture Set 2: Starting Java

Java Basic Programming Constructs

Transcription:

Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of the Debugger 17 January 2019 SP1-Lab1-2018-19.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 1

Module Information Lectures: Afternoon 2pm Birkbeck Main Building, Malet Street MAL B04 Lecturer: Carsten Fuhs Evening 6pm (surnames A-J), 7.30pm (surnames K-Z) 1-19 Torrington Place B17 Lecturer: Roman Kontchakov Lab Sessions: 3.30pm, 6pm (K-Z), 7.30pm (A-J) Birkbeck Main Building, Malet Street MAL 109 Tobi Brodie, Ping Brennan Module materials: http://www.dcs.bbk.ac.uk/~roman/sp1/ Additional Tutorials 5pm Birkbeck Main Building, Malet Street MAL 109 2

Module Information Generally, each class is split into two 90 minute sessions and, as there is a large attendance the class is also split, so the lecture and lab session you attend will be one of the following below: Main lectures 2pm, 6pm and 7.30pm, Lab sessions 3.30pm, 6pm and 7.30pm Attendance is compulsory for both Lectures and Labs and a register is maintained. Note: Lab sessions are designed to reinforce the materials covered in the previous weeks lecture so there is no difference in which lecture you attend. 3

Lab Session 1: Objectives Introduction to Java basics, the Blue J IDE and debugging As this is the first week, we are not following up from a lecture, so we can spend this session familiarising ourselves with the syntax, coding conventions and data types of Java and look at the tools we will be using for programming on the module. Basic Java syntax, rules and coding conventions Java primitive data types Commenting Blue J Application for coding in Java (free, cross-platform) Basic class construction, methods and signatures, output to the terminal window In order to do this we will end the lab session by creating two classes: HelloWorld & InterestCalculator 4

Lab Session 1: Syntax syntax rules and coding conventions Rules: Every variable declaration and assignment statement in a Java program must be terminated with a semicolon; Identifiers (variable, method and class names) are case-sensitive Identifiers can begin with an alpha character, underscore or $, however coding conventions mean we always begin with alpha characters. Reserved names cannot be used for identifiers Coding Conventions: Variable names follow camel case such as studentnumber Class names begin with a capital letter Method names begin with a lowercase letter Constants are named in CAPITALS Descriptive names are used for variables, not abbreviations ( e.g. int speed = 70; not int s = 70;) 5

Lab Session 1: Data Types When declaring variables in Java we need to set the data type as well as the variable name (and optionally, an initial value). To declare a new variable containing a whole number we use the following syntax: int studentnumber = 12311487; This tells the program there is a variable named studentnumber, which will hold an integer value. By using a single equals sign, we are showing initial assignment of the value 12311487 to this variable. Remember: values from the right of an operator are assigned to the variable on the left. Once a variable s data type is set, it will only accept values of that data type, the variable studentnumber cannot hold a value such as 2.5 6

Lab Session 1: Data Types (2) Java supports 8 primitive data types including int: byte integer values -128 127 short integer values -32,768 32,767 int default integer values -2 31 (-2,147,483,648) 2 31-1 (2,147,483,647) long default integer values -2 63 2 63-1 float - 32-bit IEEE 754 floating-point number double - 64-bit IEEE 754 floating-point number boolean values true or false char - a single 16-bit Unicode character In this module we will concentrate on the following primitive data types: int for whole numbers, double for floating-point numbers, boolean and char. 7

Lab Session 1: Data Types (3) The 8 primitive data types are written in lower case. char values are presented in single quotation marks: char initial = 't'; Boolean values are written in lowercase: boolean inlab = true; In addition to the 8 primitive data types Java provides support for character strings via the built-in String class. The string data type begins with a capital letter and values are presented within double quotations: String message = "Hello World!"; 8

Lab Session 1: Commenting Block Comments Block comments are used to provide descriptions of files, methods, data structures and algorithms. Block comments may be used at the beginning of each file and before each method. They can also be used in other places, such as within methods. Block comments inside a function or method should be indented to the same level as the code they describe. A block comment should be preceded by a blank line to set it apart from the rest of the code. /* * Here is a block comment. */ Block Comments can also be used as trailing comments: if (a == 2) { case */ } return true; /* special 9

Lab Session 1: Commenting (2) End-Of-Line Comments The // comment delimiter can comment out a complete line or only a partial line. It shouldn't be used on consecutive multiple lines for text comments; however, it can be used in consecutive multiple lines for commenting out sections of code. if (number >= 0) { // code for natural numbers... } else { return false; // number will not be in range } http://www.oracle.com/technetwork/java/javase/documentation/codeconventions- 141999.html 10

Name of project: week1 Java Project Name of class: HelloWorld 11

Getting Started Launch BlueJ - begin with the Start icon in the lower left corner of the screen. Select the options in the order shown: Start -> All Programs -> Programming Tools -> BlueJ Create a new Project on your disk space. 1. Select Project then followed by New Project. 2. Select a directory in your disk space (in drive I on non-dcs computers) and a suitable name for your project, e.g. week1. After entering week1 in the BlueJ window, a new BlueJ window will appear for the project week1. 12

Getting Started (2) Create a new class by clicking on button New Class... in the new BlueJ window. 13

Getting Started (3) Enter the name HelloWorld for the new class and click on OK. 14

Exercise 1 :Write your first class Move the mouse on top of the class icon with the name HelloWorld, right-click and select Open Editor. 15

Exercise 1 : Write your first class (2) Delete all the code in the template class and leave it empty for now. This can be easily done by selecting all (control + A), using the right click and selecting cut. 16

Exercise 1: Write your first class (3) Writing your own code: 1. Start by writing two keywords, public class. 2. Write the name of the class, HelloWorld. 3. First line of your code looks like: public class HelloWorld 4. Any code that you might write next for the class HelloWorld must be put after the first line and it must be enclosed with braces (i.e. { } ). public class HelloWorld { /* all code must lie between the two braces that define the boundaries of the class */ } The two slashes // denote the beginning of a comment. 17

Exercise 1: Write your first method Steps in defining a method: 1. First write public static void. 2. Next write the method s name main. 3. Followed by the method s parameters String[] args in brackets. 4. Finally followed by the method s boundaries ( open/close braces { } ). Your code must look like: public class HelloWorld { public static void main(string[] args) { } // end of method } // end of class Note the indentations of the lines of code which make the code easier to read. 18

Exercise 1: Write your first method (2) 5. Use the statement System.out.println(); within the method to make it print something in your terminal. For example, System.out.println("Hello, World!"); 19

Exercise 1: Write your first method (2) 6. Your code must look like this: 20

Exercise 1: Compiling your first class Click on the button Compile. The compiler will check your code for syntax errors and error messages (if any) are displayed at the bottom of the window. The final message should be one of the following. Either Class compiled no syntax errors Or an error message. Important: after each modification of the code, always compile the new code. 21

Exercise 1: Execute the method Close the Editor and return to the project s workspace. Move the mouse on top of the HelloWorld icon, rightclick and invoke the method main by clicking on it. 22

Exercise 1: Execute the method (2) After selecting the main method, a window will appear and select OK. A terminal window will appear with the message: Hello, World! 23

Note The exercise just completed outlines the basic structure for creating Java classes, creating a method within the class, compiling and finally executing the method. Generally, the same structure will be applied for classes created during the sessions for the next few weeks. Exercise 2, which follows, requires you to again create a new class, define methods within the class, then compile and execute to see the result. 24

Exercise 2: InterestCalculator You put 10,000 into a bank account that earns 5% interest per year. What will the balance be after 3 years? (Based on JFE, Section 1.7) 25

Exercise 2: InterestCalculator (2) Initial balance: 10000 Interest rate: 5% per year Interest earned after 1 year: 10000*5/100 = 500 New balance after 1 year: initial amount + interest = 10000+500 = 10000+(10000 * 0.05) Balance after each subsequent year: = previous balance + interest on it 26

Exercise 2: InterestCalculator (3) Pseudo code: 1. initialbalance = 10000 2. Print initial balance + initialbalance 3. currentbalance = initialbalance + intereston(initialbalance) 4. Print year1 + currentbalance 5. currentbalance = currentbalance + intereston(currentbalance) 6. Print year2 + currentbalance Note: The code to calculate the balance is identical for lines 3 & 5 and will be for each successive year. A method to calculate the interest should be written in addition to the main method. 27

Exercise 2: InterestCalculator (4) } public class InterestCalculator { public static void main(string arg[]){ //declare variable initialbalance //declare variable currentbalance System.out.println("The initial balance is " + initialbalance); currentbalance = initialbalance + intereston (initialbalance); //print new balance /* repeat previous 2 lines (use currentbalance instead of initialbalance as argument for intereston call) to calculate balance for 2 more years (see previous slide) */ } public static double intereston(double balance){ double interest = balance * 0.05; return interest; } 28

Exercise 2: InterestCalculator (5) A closer look at the method to calculate interest: public static double intereston(double balance) { } double interest = balance * 0.05; return interest; When writing Java methods we must declare the data type of the return value (returned by the return statement) as well as the data type of parameters passed to methods as arguments (double balance is a declaration of the variable that stores the value passed into the method from the method call) 29

Exercise 2: Compiling InterestCalculator Click on the button Compile. The compiler will check your code for syntax errors and error messages (if any) are displayed at the bottom of the window. The final message should be one of the following. Either Class compiled no syntax errors Or an error message. Important: after each modification of the code, always compile the new code. 30

Exercise 2: Execute the method Close the Editor and return to the project s workspace. Move the mouse on top of the InterestCalculator icon, right-click and invoke the method main by clicking on it. A window will appear and select OK. A terminal window will appear with output similar to below: 31