Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

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.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Chapter 7: User-Defined Functions II
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
True or false A variable of type char can hold the value 301. ( F )
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
Chapter 6 Horstmann Programs that make decisions: the IF command.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CSC204 – Programming I Lecture 4 August 28, 2002.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 8: Arrays.
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.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Control Structures II
Topic 4 Inheritance.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
CSC 212 Object-Oriented Programming and Java Part 2.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
A First Book of C++ Chapter 4 Selection.
Midterm preview.
Java Programming Fifth Edition
Suppose we want to print out the word MISSISSIPPI in big letters.
Control Structures.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5: Control Structures II
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
null, true, and false are also reserved.
Building Java Programs
Review for Midterm 3.
Building Java Programs
Presentation transcript:

Review – Final Test Chapters 8,10,11

Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input is not integer”);

try and catch blocks must be enclosed within curly braces. A corrected version:

Locate the error in the following statement and show how to fix it swithch(channel) { case 1: System.out.println(“Channel 1”); case 3: System.out.println(“Channel 2”); case 5: System.out.println(“Channel 3”); }

There should be a break statement at the end of each case: switch (channel) { case 2: System.out.println(“channel 1"); break; case 5: System.out.println(“channel 2"); break; case 11: System.out.println(“channel 3"); break;

Locate error in following statement: do i /= 2 while (i > 10);

There's no semicolon at the end of the loop body. A corrected version: do i /= 2; while (i > 10);

Which one of the following statements are not equivalent to other two: (a)while(i<10)System.out.println(i); (b)for(;i<10;)System.out.println(i); (c)Do System.out.println(i); while(i<10);

(c). All three statements are potentially infinite loops. (c) is different from the other two, however, because it always prints the initial value of i, even if i is greater than or equal to 10.

What will be printed when the following code is executed? int a = 1, b = 0, c; try { c = a / b; System.out.println("Division completed"); } catch (ArithmeticException e) { System.out.println("You can't catch me!"); } System.out.println("Time to move on");

You can't catch me! Time to move on

Which one of the following is a legal type for the controlling expression in a switch statement: (a) char, (b) double, or (c) String, or (d) none of the above?

(a) char

What is the role of the break statement inside switch statements: (a) required at the end of each case; (b) not required at the end of each case, but usually needed; or (c) not required at the end of each case, and usually not needed?

(b) not required at the end of each case, but usually needed

For each of the following situations, indicate which type of statement would be more appropriate: an if statement or a switch statement. Assume that each test will be accompanied by a different action. (a) Testing whether a String variable is equal to one of fifty different state abbreviations (such as "GA" or "AL"). (b) Testing whether an int variable is between 0 and 59, between 60 and 69, or between 70 and 100. (c) Testing whether an int variable matches one of 50 different TV channels. (d) Testing whether a double variable matches one of the values 0.0, 0.5, 1.0, 1.5,..., (e) Testing whether the length of a String variable matches one of the values 1, 2, 3,..., 10.

(a) if statement (b) if statement (c) switch statement (d) if statement (e) switch statement

For each of the following properties, answer – I if the property applies only to instance methods, – C if it applies only to class methods, – B if it applies to both, and – N if it applies to neither. (a) Can be public or private (b) Can access instance variables (c) Can access class variables (d) Can have parameters (e) Can call instance methods in the same class (f) Can call class methods in the same class (g) Can be overloaded (h) Can use the keyword this (i) Must be called by an object (j) Calls must always include a dot (.)

B(a) Can be public or private I(b) Can access instance variables B(c) Can access class variables B(d) Can have parameters I(e) Can call instance methods in the same class B(f) Can call class methods in the same class B(g) Can be overloaded I(h) Can use the keyword this I(i) Must be called by an object N(j) Calls must always include a dot (.)

Given the Account class, create a Checking Account class by extending Account class. The CheckingAccount will store the number of checks written against the account. It will need a method that returns the number of checks written so far and a method that writes a check for the given amount. public class Account { private double balance; public Account(double initialBalance) { balance = initialBalance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } }

public class CheckingAccount extends Account { private int checksWritten = 0; public CheckingAccount(double initialBalance){ super(initialBalance); } public int getChecksWritten() { return checksWritten; } public void writeCheck(double amount) { withdraw(amount); checksWritten++; }

For a Complex class that stores a complex number with real and imaginary parts in instance variables realPart and imaginaryPart respectively, write a an equals method that for the complex class. For two complex numbers to be equal, they must have the same real part and the same imaginary part.

public boolean equals(Object obj) { if (!(obj instanceof Complex)) return false; Complex c = (Complex) obj; return realPart == c.realPart && imaginaryPart == c.imaginaryPart; }