Lecture 5. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Java Planning our Programs Flowcharts Arithmetic Operators.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
1 Lecture Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Lecture 3. Review (What is Class and Object ?) The world of JAVA contains objects The world of JAVA.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Lecture 6. What is the difference in pictures ???
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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;
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Shorthand operators.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
1 Java Basics: Data Types, Variables, and Loops “ If debugging is the process of removing software bugs, then programming must be the process of putting.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mixing integer and floating point numbers in an arithmetic operation.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Building java programs, chapter 3 Parameters, Methods and Objects.
By Mr. Muhammad Pervez Akhtar
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
Introduction to java (class and object). Programming languages: –Easier to understand than CPU instructions –Needs to be translated for the CPU to understand.
Chapter 2 Clarifications
Today’s topic: Arithmetic expressions.
OPERATORS (1) CSC 111.
OPERATORS (2) CSC 111.
MSIS 655 Advanced Business Applications Programming
Building Java Programs
CS 200 Primitives and Expressions
Building Java Programs
Presentation transcript:

Lecture 5

Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// … /////// Method /////// … }

Review (Variables) A variable is a symbol representing a value public class Turtle { ///// Field (Attributes) ///// int length; int position = 100; double size = 2.5; ///// Method ///// … }

Review (variable declaration) What does this line mean??? We declare a variable, named length The type of value should be int Then, initialize the variable with 15 int length = 15; data type variable name length 15 value

Review (arguments/parameters) A argument is a variable that will be entered in a method as input value public class Turtle { … /////// Method /////// public void drawLine(int len) { penDown(); forward(len); } }

Today’s topic More about methods  Methods in a method (sub-method)  Main method Arithmetic expression  +, -, *, /, %

Methods in a method (“sub-methods”) You can use (call) methods in a method as sub-methods public class Turtle { public void drawLine(int len) { forward(len); } public void drawSquare(int len) { drawLine(len); turnRight(); } }

Practice You already have two method –drawTriangle, drawRectangle drawTriangle receives 1 parameter drawTriangle(int len) drawRectangle receives 2 parameters drawRectangle(int width, int height)

Practice (drawSimpleHouse) Please write another method – Name: drawSimpleHouse – Receive 2 parameters, width and height – Use 2 methods in the previous slide You can also use penUp(); penDwon(); moveTo( x, y ); x y

Practice (sample code) public void drawSimpleHouseLine(int w, int h) { penUp(); moveTo(200, 200); penDown(); drawRectangle(w, h); drawTriangle(w); } x y For example, you may type t.drawSimpleHouse(150, 100);

Main method public class Test { public static void main(String[] args) { } Look at Test.java

Source code Java compiler Java bytecode Java interpreter Bytecode complier Machine code.java file.class file Execute! Execute program from main method We can test (in Interaction panel)

Main method public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawSimpleHouse(); } Let’s write main method and RUN it !!!

How to execute a program? When a program is executed, it always starts from a main method Right click on the file containing a main method

Arithmetic Expression!!! How we can do Math in Java program ???

Arithmetic expression We can calculate arithmetic expression e.g., 2 + 3, 5 – 3, 4 * 5, 6 / 2 An expression is a combination of one or more operators and operands Addition + Subtraction - Multiplication * Division / Remainder% 5 + 3

Assignment The result of expression is assigned to a variable by using = symbol x = 5 + 3; int x; Variable declaration Assignment Note: Somewhat different from math Variable x contains a value of 8

Assignment (more) Variables can be operands Variables may be re-assigned x = 5 + 3; int x; Variable declaration Assignment int y; Variable declaration y = x – 4; Assignment x = x / y; Assignment

Printout Method We can print out the variable value to a screen by calling a method System.out.println( variable );

Practice (Printout Method) public class Test { public static void main(String[] args) { int x; int y; x = ; y = 291 / 3; System.out.println( x ); System.out.println( y ); } Where the results appear ???

Exercise 1 By running Test class, (i.e. calling a main method) Calculate your age in minutes and show the result to a screen use following conversions (assumptions) 1 year = 365 days 1 day = 24 hours 1 hour = 60 minutes

Exercise 1 (sample code) public class Test { public static void main(String[] args) { int minutes; minutes = 17 * 365 * 24 * 60; System.out.println( munutes ); }

How about this? (Useful!!!) public class Test { public static void main(String[] args) { int age = 17; int minutes; minutes = age * 365 * 24 * 60; System.out.println( munutes ); }