CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Similar documents
CS100J Prelim I, 29 Sept. 2003

C a; C b; C e; int c;

Java Object Oriented Design. CSC207 Fall 2014

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

CS 1302 Chapter 9 (Review) Object & Classes

Chapter 7. Inheritance

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Lecture 10. Overriding & Casting About

Exam Duration: 2hrs and 30min Software Design

Introduction to Programming Using Java (98-388)

PREPARING FOR THE FINAL EXAM

Distributed Systems Recitation 1. Tamim Jabban

CS100J Prelim 1 22 February 2007

Basic Object-Oriented Concepts. 5-Oct-17

Logistics. Final Exam on Friday at 3pm in CHEM 102

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

PREPARING FOR PRELIM 1

Chapter 6 Introduction to Defining Classes

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

PREPARING FOR PRELIM 2

CS111: PROGRAMMING LANGUAGE II

Example: Count of Points

Readings for This Lecture

type conversion polymorphism (intro only) Class class

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

First IS-A Relationship: Inheritance

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

9/10/2018 Programming Data Structures Inheritance

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

CS 61B Discussion 4: Inheritance Fall 2018

Handout 4 Conditionals. Boolean Expressions.

CSE 113 A. Announcements - Lab

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CS 251 Intermediate Programming Methods and Classes

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Csci 102: Sample Exam

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Inheritance & Polymorphism

Announcements for the Class

CS 112 Introduction to Programming

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Classes in Java Part I

CLASS DESIGN. Objectives MODULE 4

Informatik II Tutorial 6. Subho Shankar Basu

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance. Transitivity

Chapter 4: Writing Classes

Instance Members and Static Members

OVERRIDING. 7/11/2015 Budditha Hettige 82

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS 251 Intermediate Programming Methods and More

Inheritance (Outsource: )

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Java Primer 1: Types, Classes and Operators

Big software. code reuse: The practice of writing program code once and using it in many contexts.

OBJECT ORİENTATİON ENCAPSULATİON

Inheritance & Polymorphism. Object-Oriented Programming

C# Programming for Developers Course Labs Contents

Chapter 15: Object Oriented Programming

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

16 Multiple Inheritance and Extending ADTs

CSC9T4: Object Modelling, principles of OO design and implementation

Informatik II (D-ITET) Tutorial 6

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

What are the characteristics of Object Oriented programming language?

[TAP:AXETF] Inheritance

CS1004: Intro to CS in Java, Spring 2005

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS1020: DATA STRUCTURES AND ALGORITHMS I

Arrays Classes & Methods, Inheritance

Lecture 2: Java & Javadoc

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CS-202 Introduction to Object Oriented Programming

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Java Programming Lecture 7

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Casting. References. References

More Relationships Between Classes

INHERITANCE. Spring 2019

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Use the scantron sheet to enter the answer to questions (pages 1-6)

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

Java Identifiers, Data Types & Variables

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

Transcription:

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM This handout explains what you have to know for the first prelim. Terms and their meaning Below, we summarize the terms you should know. You should be able to state the definition of a term like assignment statement clearly and precisely. For a Java construct, you should know its syntax. For a Java statement, you should know how to execute it. Expressions: Types int, double, boolean, char (their ranges and basic operations). Casting between types. Narrower type, wider type. * Methods: Two kinds of methods: procedure, function. Syntax of a method definition. Parameter of a method. Local variable of a method. Scope of a parameter and a local variable. Be able to write a simple method. Method calls: How to call each kind of method. Argument of a method call. Restrictions on arguments based on the type of the corresponding parameter. Frame for a method call Variables. variable, declaration of a variable, assignment statement. If-statement and if-else statement. What their syntax is and how they are executed. If-condition. Block. What its syntax is and how it is executed. Classes. What is a class? Class definition. Instance (folder, or object) of a class. The name of a folder. Components: fields and methods. Static and non-static components of a class (where do they go?). What this means. The new-expression and what it is used for. You should be able evaluate a new-expression by hand, drawing the new folder, filling in the initial values, and yielding the name of the new folder. Subclasses.How to define a subclass. Inheritance and overriding. Casting with class-types. Narrower and wider class types. You should be able to draw a folder of a class or subclass, given the class definition. Key concepts These notes contain short definitions of the basic entities that make up a Java program, along with a description of the Java syntax for them and examples of them. Memorize these definitions. You should know them backward and forward by now, for they form the backbone of whatever we do from now on. On a test, you should be able to write such definitions and examples. What you write must be precise and clear. Class: A file drawer: contains static components and folders (instances, objects) of that class. Class definition: a model, form, or blueprint for the objects (or instances) of the class; a class defines the components of each object of the class. All folders of the class have the same components. Analogy: a blueprint for a house is a design for a house, many houses (objects) can be built from the same blueprint, but they may differ in color of rooms, wallpaper, etc. public class <class name> { declaration of fields and methods Variable: A named box that can contain a value of some type. For a type like int, the value is an integer. For a class-type, the value is the name of (or reference to) an instance of the class the name that appears on the folder. Declaration of a variable: a definition of the name of the variable and the type of value it can contain. <class or type name> <identifier> ; Examples of variable declarations: A variable x that can contain an integer: int x;

A variable s that can contain the name of an object of class String: String s; A variable c that can contain a boolean value (true or false): boolean b; Method: A parameterized sequence of statements, whose execution performs some task. We study (so far) two kinds of methods: procedures and functions. A method should be accompanied by a comment that says what the method does. This is the specification of the method. The comment has to be precise and clear. A potential user of the method should be able to look only at the comment and the list of parameters to know how to call the method; they should not have to look at the body of the method. Example. When you want to bake a cake, you look at the title of a recipe, a short description, and the list of ingredients to determine whether you want to use that recipe not the list of instructions to bake it. A procedure is a method that performs some task (and doesn t return a value) /** Comment that explains what the method does */ public void <method name> (<parameters>) { Sequence of statements to execute /** Raise the salary by n dollars if the salary is < $20000 */ public void raisesal(double n) { if (salary < 20000) salary= salary + n; Example procedure call: raisesal(20*y); A function is a method that performs some task and returns a value. Instead of keyword void, the type of the returned value is used. Statement return <value>; is used to terminate execution of a function call and return <value>. /** Comment that explains what the function does. It should include something like = to describe what the function value is. */ public void <method name> ( <parameters> ) { Sequence of statements to execute /** Yield the maximum of x and y */ public int max (int x, int y) { if (x>= y) return x; return y; Example of a function call of max (within some statement): z= 1 + max(x,y); Execution of an assignment statement stores a value in a variable. <variable name> = <expression> ; Restriction: The type of the expression cannot be narrower than the type of the <variable name Examples: b= 2+c; s= Cardie + + yearhired ;

Please, always put no blanks before = and one blank after =, to make it look unsymmetric and remind you that it is not an equality test but an assignment. A block is used to unify a sequence of statement into a single statement. { sequence of statements Here is a sequence of two statements: a= 10; if (a < c) then a= c; Here is a single statement, which is a block { a= 10; if (a < c) then a= c; Execution of a conditional statement allows a choice of execution. if ( <boolean expression> ) <statement> or if ( <boolean expression> ) <statement 1> else <statement 2> The first form is executed as follows: if <boolean expression> is true, then execute <statement> The second form is executed as follows: if <boolean expression> is true, then execute <statement 1>; if the <boolean expression is false, then execute <statement 2>. A subclass B (say) is a class that extends another class A (say). This means that an instance of B has all the fields and methods that an instance of A has, in addition to the ones declared in B. public class <class name> extends <class name> { declarations of fields and methods Access modifiers. Suppose d is an instance of Employee, where class Employee is declared as: public class Employee { <access modifier> int x; If the <access modifier> is: public, then field d.x can be referenced anywhere that d can be referenced. If private, then field d.x can be referenced anywhere within class Employee that d can be referenced kinds of variables: local variables, parameters, and fields (non-static) public class Class1 { public int x; public int y; public voidclass1 (int z) {z= z; y= 2*z; // x is a field of instance variable. It appears in every folder // z is a parameter. // Set y to the maximum of p and p public void sety(int p) { // p is a parameter int x; // x is a local variable of method sety. It cannot be used

x= p; if (p > p) // outside the method. It is local to the method. The scope of a name is the set of places in which it can be referenced. A variable can be declared only once within a method. Such a variable is sometimes called a local variable (of the method). The scope of a local variable of a method is the sequence of statements following it. /** specification of method */ public test(int p) { int x; // The scope of x starts at the next statement and goes x= p; // to the end of the block in which the declaration of x appears if (p > p) The scope of a parameter of a method is the method body. //... public test(int p) { // The scope of parameter p is the method body if (y= p); { int x; // The scope of x starts at the next statement and x= p; // goes until the end of the block in which the declaration if (p > p) // of x appears. It does not include the last statement y= p. The scope of a field of a class consists of: (1) the bodies of all methods declared in the class and (2) all declarations of fields that follow the declaration of the field. public class Text { x int x= 5; int y= x+15; public void test(int p) { if (x= p); { int x= 35; x x= p; if (p > p) x= p; Sample questions Below, we give some sample questions. Note that you may be asked to write a small procedure or function, using assignments, if-statements, blocks, and return statements. Two sample questions of this nature appear

at the end of the sample questions. 1. a) When do you use "=" and "==" b) what is the difference between 'c' and "c" c) if b == true and c =="true" what is the type of the variables b and c? d) What is the difference between a method declared with keyword static and one without the keyword? 2. Below is a class. Draw a folder of this class. public class A{ public static void main(){ int a= 2 ; int b= negate(negate(a)) ; public static int negate(int x) { return( x) ; 3. Find the various syntax and semantic errors in the code given below: public class A{ public static void main() { String a= "true" String b= false ; int d = diffinlength(a) public static boolean diffinlength(string s1, String s2) { return(abs(s1.length s.length())) 4. Starting with values a=5 b=23 c=7 d=0 b1=true b2=false Find the values of a, b, c, d. Start with the above values for EACH item. (a) if((a%a)==d) (b) d= b/c ; d= 3; a= a d ; (c) a= a*5 ; (d) if (b1) b2= true; a= a 5 if (b2) b1= false; (e) if (b1 (a!=3)) b2= true 5. Give the syntax of the assignment statement and write down how to execute it. 6. Give the syntax of a block and explain how to execute it. 7. Below is a class Employee. public class Employee { private String name; // employee s name private Date hiredate; // date employee was hired /** Constructor: employee named n hired on date d public Employee (String n, Date d) { /** = name of the employee */ public String getname() { /** = hiredate */ public Date gethiredate() {

/** = a representation of the employee, giving their name and date of hire *./ public String tostring() { (a) Write the three method bodies (but not the constructor body). (b) Write a new-expression to create an Employee with name Roger who is hired at the time the newexpression is evaluated. Draw the manilla folder that represents the newly created object. 8. Look at class Employee of the previous exercise. (a) Write a subclass VIP that has a field, bonus, which contains a double value. The subclass needs a constructor that initializes all three fields. Make sure you write the body of the constructor correctly. The subclass should have its own tostring function and a getter method for the bonus. (b) Which components does subclass VIP inherit? Which does it override? (c) Write another constructor in subclass VIP that has only one parameter, the name of the person. The bonus should be 0 initially, and the date of hire should be 1 february 1979. 9. Consider a class Animal: public class Animal { private String kind; // kind of animal --e.g. "cat" private String name; // the animal's name // Constructor: an instance with kind k and name s public Animal(String k, String s) { kind= k; name= s; // = description of this Animal public String tostring() { return "Name: " + s + ", kind " + k); Here's an expression that creates an instance: new Animal("cat", "softy"); Write a subclass Lion of Animal that represents lions and also gives their age. The only instance variable of the subclass should be an int variable that contains the age. The constructor should have two parameters the age and the name. You do NOT have to write the body of the constructor. There should be a getter method for obtaining the age and a getter method for obtaining the name. Override method tostring so that a call on it returns a description with all three properties kind, name, and age. This method should contain a call on the constructor of the superclass. 10. Write the following method. /** = the larger of x*y, x*x, and y*2*y */ public static int larger(int x, int y) { 11. Given are three int variables x, y, and z. Write a sequence of Java statements to put the larger value in x, the middle value in y, and the smaller value in z.