1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

Slides:



Advertisements
Similar presentations
Basic Java Constructs and Data Types – Nuts and Bolts
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
16-Jun-15 javadoc. 2 Javadoc placement javadoc comments begin with /** and end with */ In a javadoc comment, a * at the beginning of the line is not part.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.
1 Tirgul no. 4 Topics covered: H Defining classes H Documentation using javadoc H The computer memory during program execution.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Chapter 4: Control Structures II
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Objects and Classes Mostafa Abdallah
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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.
Variable Scope & Lifetime
Object-Oriented Concepts
Classes and Objects Introduced
Chapter 7 User-Defined Methods.
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 6 More Conditionals and Loops
Selected Topics From Chapter 6 Iteration
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Chapter 6 More Conditionals and Loops
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
CHAPTER 6 GENERAL-PURPOSE METHODS
Introduction to javadoc
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Special instance methods: toString
Chap 2. Identifiers, Keywords, and Types
Additional control structures
First Semester Review.
Presentation transcript:

1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

2 for loop for ( statement1; condition; statement2) { statements; } Where statement1 the condition and statement2 may be empty or contain multiple statements.

3 while loop while (boolean-condition) { statements; } do { statements; } while(boolean-condition);

4 for example public class For { public static void main(String args[]) { int i; String s1,s2; //endless loop for(;;) { System.out.println("q"); } //a very bad example of using a for loop for(i=0,s1=new String("a"),s2=new String("b"); i<3; i++,s1+="q",s2+="z") { System.out.println(s1); System.out.println(s2); }

5  Compute e (  1/n!) public double computeE1() { double answer = 0; int fact = 1; final int iterations = 20; for(int i=0; i<iterations; i++) { answer += 1.0/fact; fact *= i+1; } return answer; }

6  Compute e (  1/n!) (cont.) public double computeE2() { final double epsilon = ; double answer = 0; int i=0,fact = 1; while(1.0/fact > epsilon) { answer += 1.0/fact; i++; fact *= i; } return answer; }

7 Reversing an integer public class ReverseInt { public static void main(String args[]) { int src,rev; System.out.print("Please enter an integer: ”); src = EasyInput.readInt(); rev = 0; while(src != 0) { rev= (rev*10) + (src%10); src/=10; } System.out.println(rev); }

8 Nested Loops public static void main(String[] args) { for(int i=1; i<=9; i++) { for(int j=0; j<=Math.abs(i-5); j++) System.out.print("*"); System.out.println(); } ***** **** *** ** * ** *** **** ***** Print this pattern:

Call to checkValue( ) (as an expression): x.checkValue(3,7,9) assigns 3, 7, and 9 to the formal parameters col, row, and limit, respectively; then executes the method Formal Parameters of a Method boolean checkValue (int col, int row, int limit) returned type of method name of method three formal parameters of method, each of type int

10 Magic Numbers Magic Number: A magic number is a number whose sum of cubes of digits equals the number itself. For example, 153 is a magic number since = 153. public class MagicNumbers { public static void main(String[] args) { System.out.print("Enter a value N to determine the range 1..N ”); int n = EasyInput.readInt(); for (int i=1; i<n; i++) { if (isMagic(i)) { System.out.println(i); }

11 Magic Numbers (cont.) // Checks if a given number is a magic number static boolean isMagic(int n) { int sumOfCubes = 0; int number = n; // keep the original number while (number>0) { int digit = number%10; sumOfCubes += digit*digit*digit; number /= 10; } return (n == sumOfCubes); }

12 Defining New Classes /* * This is a basic date object. For now it cannot do anything * except hold the initial date it is given in creation. */ public class Date { private int day; // the day in month, an integer variable private int month; private int year; /* * This is a constructur. This function is called when a new * object of type Date is created. It receives three parameters * that define the date. Note that the parameters are * assumed to be valid. */ public Date(int day, int month, int year) { this.day = day; this.month = month; this.year = year; }

13 Using the Class We Wrote /* * This is a small application that shows how to use Date objects. */ public class DateTest { /* * This is a special method. If this class is run by java then the * excecution is started here. */ public static void main(String[] args) { Date d1; // declare a variable that refers to a Date object. // create a new Date objecet with the date 23/6/1997. // and make 'd' refer to it. d1 = new Date(23, 6, 1997) ; // we can also declare a variable and create // the object in one line: Date d2 = new Date(14, 4, 1998); }

14 Extending the Classes Functionality /* * This is a basic date object. For now it cannot do anything * except hold the initial date it is given in creation. */ public class Date { private int day; // the day in month, an integer variable private int month; private int year; /* * Same as previous example. */ public Date(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } /** * This method prints the date to standard output. */ public void print() { System.out.println(day + "." + month + "." + year); }

15 Get and Set methods /* * The following methods are used to set and get the values of private * fields of the object. */ /* * set the value of the day. */ public void setDay(int newDay) { day = newDay; } /* * return the current day in the month. */ public int getDay() { return day; } /* * The setMonth(), getMonth(), setYear(), getYear() methods * are similar. */

16 Using Our Class public class DateTest { public static void main(String[] args) throws Exception { Date d1 = new Date(12, 11, 1997); Date d2 = new Date(1, 1, 1998); int day; System.out.print("the date d1 is: "); // don't move to next line d1.print(); // ask d1 to print itself System.out.print("the date d2 is: "); d2.print(); System.out.print("Please enter a new day for d1: "); day = EasyInput.readInt(); d1.setDay(day); // change d1's day. d2 is unchanged. // print again System.out.print("the date d1 is: "); // don't move to next line d1.print(); // ask d1 to print itself System.out.print("the date d2 is: "); d2.print(); }

17 Circle Class public class Circle { private double centerX; // the center of the circle private double centerY; private double radius; // the radius of the circle public Circle(double x, double y, double radius) { centerX = x; centerY = y; this.radius = radius; } public boolean isIn(double x, double y) { //use the Math object's power and square root methods to do //the calculation. double dist = Math.sqrt(Math.pow((centerX-x), 2.0) + Math.pow((centerY - y), 2.0)); // now check if point is in circle or not if (dist <= radius) { return true; } else { return false; }

18 Using our Circle Class public class PointInCircle { public static void main(String[] args) throws Exception{ double x,y; double radius; //read input from user and create circle System.out.print("Enter the circle center (x y): "); x = EasyInput.readDouble(); y = EasyInput.readDouble(); System.out.print("Enter the circle radius: "); radius = EasyInput.readlnDouble(); Circle cir = new Circle(x,y,radius); System.out.print("Enter X and Y coordinates (use space):"); x = EasyInput.readDouble(); y = EasyInput.readlnDouble(); if(cir.isIn(x,y)) { System.out.println("The point is in the circle"); } else { System.out.println("The point is not in the circle"); }

19 Javadoc parses the declarations and documentation comments in a set of Java source files and produces a corresponding set of HTML pages describing (by default) the public and protected classes, inner classes, interfaces, constructors, methods, and fields. You can include documentation comments in the source code, ahead of declarations for any entity (classes, interfaces, methods, constructors, or fields). These are also known as Javadoc comments, and the text must be written in HTML, in that they should use HTML entities and can use HTML tags. How to run: javadoc MyClass.java The result includes several HTML files. We are only interested in the file MyClass.html. Javadoc (from sun’s javadoc documentation)

20 Javadoc (cont.) A doc comment consists of the characters between the characters /** that begin the comment and the characters */ that end it. Javadoc parses special tags when they are embedded within a Java doc comment. These doc tags enable you to autogenerate a complete, well-formatted API from your source code. Important Javadoc - author of - parameter - return value description. Example: /** * This method receives the new circle radius. radius new circle radius. */ public setRadius(double radius) { this.radius = radius; }

21 Example of javadoc documentation /** * The Point class represents a 2-dimensional integral point. Ziv Yaniv */ public class Point { private int x; // x-coordinate of this point private int y; // y-coordinate of this point /** * Construct a new point with the specified x y coordinates. x The x coordinate. y The y coordinate. */ public Point(int x, int y) { this.x = x; this.y = y; } /** * Return the x-coordinate of this point. */ public int getX() { return x; } /** * Set the x-coordinate of this point to be the given value. newX The new x-coordinate. */ public void setX(int newX) { x = newX; }

22