Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
Hello, world! Dissect HelloWorld.java Compile it Run it.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Lecture 2: Classes and Objects, using Scanner and String.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CompSci 230 S Programming Techniques
Key Words / Reserved Words
Chapter 2 Variables.
Chapter 2 Basic Computation
Intro to ETEC Java.
Elementary Programming
Building Java Programs
Lecture 5: Some more Java!
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Testing and Debugging.
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Chapter 2 Basic Computation
Object-Oriented Programming
Starting JavaProgramming
An Introduction to Java – Part I, language basics
Introduction to Java, and DrJava part 1
Building Java Programs
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
Sridhar Narayan Java Basics Sridhar Narayan
Introduction to Java, and DrJava
Introduction to Java Brief history of Java Sample Java Program
Chapter 2 Programming Basics.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Primitives
Building Java Programs
In this class, we will cover:
Unit 3: Variables in Java
CS Week 2 Jim Williams, PhD.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
LCC 6310 Computation as an Expressive Medium
Building Java Programs
Presentation transcript:

Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date

Eclipse IDEs have lots of support to make programming easier Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger Eclipse is an Integrated Development Environment (IDE). IDEs have lots of support to make programming easier Auto-complete and hints Real time compiling Project management Debugging support A zillion other things and they are extensible There are lots of different IDEs, and lots of Java ones. Some are designed for a particular type of development (e.g. cross-platform apps). Some are proprietary (e.g. Visual Studio is Microsoft’s IDE for all their programming languages) IDEs are big and complex, but definitely worth the investment for the support they provide.

Eclipse basics 1 2 3 Eclipse IDE Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger All programs must be in their own project 1 2 3

Add a class Eclipse IDE Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger

Empty Class Eclipse IDE Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger

The payback Eclipse IDE Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger Intelligent support when you push . e.g., System. Real time syntax checking and hints Debugging support (we will do that later)

Run your program Eclipse IDE Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger

Right click and choose reset Note: Fixing the views Eclipse IDE Primitive variables Getting input Arithmetic Flow control with if-else Eclipse Debugger Eclipse has lots of windows and they float, open close etc. Really easy to mess it up! You want the Java Perspective (Window/Perspective/Open Perspective/Java Browsing) Reset perspective Right click and choose reset

Simple console output Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger Before we start looking at variables, a few words about output: // System.out.println() outputs its argument System.out.println(); // prints just a newline System.out.println(42); // prints 42 and a newline System.out.print(42); // prints just 42 System.out.print("Hello!") // prints Hello System.out.print(42 + 8); // prints 50 System.out.print("Formula " + 1); // prints Formula 1 System.out.print("Hello " + "World"); // prints Hello World // System.out.printf() got ported from C. It uses a string // with format codes (such as %d or %.1f) to output data just // the way the programmer wants. Examples follow!

Note the printf() here – output an int (sum) with %d Variables import java.util.Scanner; public class AddTwoNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number1; int number2; int sum; System.out.println("Input an integer: "); number1 = input.nextInt(); number2 = input.nextInt(); sum = number1 + number2; System.out.println("Sum is " + sum ); System.out.printf("Sum is %d", sum); /* note if you get an error on printf change your compiler compliance level by Project> Properties> Java Compiler (to 1.5 or 1.6) */ } Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger Note the printf() here – output an int (sum) with %d

Declaring Variables Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger ……… int number1; int number2; int sum; …….. All variables must be declared before they are used. Java variables are statically typed – i.e. you cannot change a variable’s type during the execution of the program (“runtime”). Variables can be declared pretty much anywhere inside a class. Where they are declared sets their scope Scope is the current block, i.e., inside the { } in which the variable is declared. The variable is not visible outside this block. Note: public class and object variables are visible outside the block in which they are declared, but need to be accessed via the class or via their object (we’ll come to that later)

Primitive types Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger // most common numeric types int number1 = 2; //32 bit integer long number2 = 3l; //64 bit integer float number3 = 2.4f; //32 bit floating point double number4 = 4.5; //64 bit floating point // smaller integer (not used much) byte number5 =127; //8 bit integer short number6 = 32000; //16 bit integer // others boolean trueFalse = true; //value true or false char character = 'a'; //16 bit unicode character There are 8 primitive types See the literals for each above We will come to String later (it is not a primitive)

Getting input Import the Scanner class from the java.util.Scanner package. Packages are bits of existing code (often provided with Java) that provide us with functionality we need. In this case, we need a Scanner object that lets us read input from the keyboard. The package provides us with the Scanner class we need to construct the object. Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger import java.util.Scanner; …… Scanner input = new Scanner(System.in); ……. System.out.println("Input an integer: "); number1 = input.nextInt(); number2 = input.nextInt(); Create (“instantiate”) a new Scanner object from the Scanner class (which acts like a blueprint). For this, we use the keyword new. We store (a reference to) the new Scanner object in the variable input, which is of type Scanner. Note: Every class defines a type, and variables of a class type can store references to objects of that class. Here, we invoke the nextInt() method on our Scanner object to give us the next integer value that the user inputs on the keyboard. We store that integer value in the integer variable number1

Arithmetic Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger ……… sum = number1 + number2; …….. Arithmetic operators and precedence First () Brackets Second * multiply / divide % remainder Third + add - subtract Finally = assignment More operators in the next lecture Worth knowing: A lot of the most frequent operators in Java are the same as in python!

Arithmetic (integer) Eclipse IDE Variables –primitives Getting input Flow control with if-else Eclipse Debugger Exercise: What are the answers? total = 3+ 5 * 2; total = (3+ 5) * 2; total = 3 * 5 + 2; total = 5 % 3 + 4; Make up some more If you can’t find the answer by thinking about it, don’t ask. Instead, try with your own piece of code!

if statements Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger import java.util.Scanner; public class Ifs { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number1; int number2; int sum; System.out.println("Input an integer: "); number1 = input.nextInt(); number2 = input.nextInt(); if (number1 == number2) System.out.println("They are the same"); else if (number1 < number2) System.out.println("The first number is smaller"); else System.out.println("The first number is bigger"); }

Relational Operators Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger ….. if (number1 == number2) System.out.println("They are the same"); else if (number1 < number2) System.out.println("The first number is smaller"); else System.out.println("The first number is bigger"); ……. Relational operators == equal (don’t get caught out and put a single =) < less than > Greater than <= less than or equal >= greater than or equal

Common error Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger if (number1 == number2); System.out.println("They are the same"); This ; terminates the if-statement The println() will always be executed.

if-else with blocks Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger if (number1 == number2) { System.out.println("The numbers are the same"); } else { System.out.println("The numbers are different");

Caution trap! This probably won’t compile. Why? Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger if (number1 == number2) { System.out.println("The numbers are the same"); int numberToUse = number1; } else { System.out.println("The numbers are different"); int numberToUse = 0; System.out.println("Using number " + numberToUse); This probably won’t compile. Why?

Bugs Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger In Java, you can have three types of problem: Your classes don’t compile. This is always the result of a syntax error or the failure to import the right package(s) Usually, javac will tell you what is wrong Sometimes, your mistake may not be in the line that javac points out. Examples: The previous statement does not end with a semicolon, you forgot to declare a variable, didn’t close a parenthesis, or you are trying to access a variable out of scope. Your classes compile, but your program crashes with an exception. Your classes compile and your program doesn’t crash, but it doesn’t do what you expect it to do. In the last two cases, using a debugger can help.

Using the Debugger Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger Set a break point on the first line of your code (right click in the blue margin beside the line) Start the debugger (F5)

Debug perspective Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger Step through line by line (F5) Check variable values by cursor over Change back to Java perspective

What do we know All variables must be declared before they are used Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger Eclipse (and other IDEs) take a little effort to learn but it’s worth it Variables All variables must be declared before they are used Variables are statically typed at the time you declare them and are only visible inside the {} of declaration There are 8 primitive data types (and String isn’t one of them) the main ones are int, double and boolean You can get input from the console with the java.util.Scanner class Arithmetic Java has the standard operators and orders of precedence (more operators next lecture) Flow control with if-else statements Java has the standard operators Be very careful not to put a ; at the end of your if(x==y) because that will terminate the if Debugger will let you step through your code and inspect the values in the variables.

Resources & Homework Get Java and Eclipse working on your machine. Eclipse IDE Variables –primitives Getting input Arithmetic Flow control with if-else Eclipse Debugger D&D chapter 2 - read it. Oracle https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html Eclipse IDE quick start tutorial https://www.youtube.com/watch?v=23tAK5zdQ9c Debugging tutorial (don’t worry about understanding the code yet) https://www.youtube.com/watch?v=Jyc2brswNQo Homework Get Java and Eclipse working on your machine. Write and run HelloWorld Do the Chapter 2 self review questions

Next Lecture D&D Chapters 3 & 4 if statements Loops More operators