Java Programming Prelim I Study Material, Study notes of Computer Science

Various topics for java programming prelim i exam, including vector/string class, recursive functions, abstract classes, and methods. It includes code examples and explanations for functions such as rev(), a recursive function that reverses the digits of a number, and the implementation of the documentary, trailer, and short classes that extend the movie class. It also discusses the concept of abstract classes and methods and their use in preventing the creation of unidentified flying animals (ufa) and ensuring that real animals have the ability to move and eat.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-zur
koofers-user-zur 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3/8/09
1
CS#1110#
Prelim#II:#Review#Session#
1#
Introduc8on#
My#name:#Bruno#Abrahao#
We#have#three#addi8onal#TA’s#in#the#room#to#help#
you#individually#
Shuang##
Nam#
Yookyung#
You’re#welcome#to#ask#them#ques8ons#at#any#8me#
2#
Exam#Info#
Prelim#II:#7:30–9:00PM,#Thursday,#12#March,#
Uris#Hall#G01#
Look#at#the#previous#Prelims#
Arrive#early!#Helps#reducing#stress#
Grades#released#the#same#evening#(morning…)#
3#
Regrade#Requests#
Releasing#grades#quickly#is#good#for#you#—#
exams#serve#two#purposes:#
Give#feedback#to#student#and#teacher#
Give#grades#
That’s#one#reason#we#
grade#~180#exams#so#
quickly#
4#
Review#session#
Let’s#make#this#interac8ve#
More#fun#
Presenta8on#is#at#slower#pace#than#a#regular#
lecture#
Ask#ques8ons##
All#ques8ons#are#smart#ones#
5#
What’s#in#the#exam?#
Everything#you#needed#to#know#for#Prelim#I#
Vector#/#String#class,#func8ons#
Wri8ng#func8ons#
Recursive#Func8ons#
apparent/real#classes,#cas8ng,#operator#
instanceof,#func8on#equals##
Abstract#classes#and#methods#
6#
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Programming Prelim I Study Material and more Study notes Computer Science in PDF only on Docsity!

CS 1110

Prelim II: Review Session

1

Introduc8on

  • My name: Bruno Abrahao
    • We have three addi8onal TA’s in the room to help

you individually

  • Shuang
  • Nam
  • Yookyung
  • You’re welcome to ask them ques8ons at any 8me

2

Exam Info

  • Prelim II: 7:30–9:00PM, Thursday, 12 March,

Uris Hall G

  • Look at the previous Prelims
  • Arrive early! Helps reducing stress
  • Grades released the same evening (morning…)

3

Regrade Requests

  • Releasing grades quickly is good for you —

exams serve two purposes:

  • Give feedback to student and teacher
  • Give grades

That’s one reason we

grade ~180 exams so

quickly

4

Review session

  • Let’s make this interac8ve
    • More fun
  • Presenta8on is at slower pace than a regular

lecture

  • Ask ques8ons
    • All ques8ons are smart ones

5

What’s in the exam?

  • Everything you needed to know for Prelim I
  • Vector / String class, func8ons
  • Wri8ng func8ons
  • Recursive Func8ons
  • apparent/real classes, cas8ng, operator

instanceof , func8on equals

  • Abstract classes and methods

6

What’s in the exam?

  • Everything you needed to know for Prelim I
  • Vector / String class, func8ons
  • Wri8ng func8ons
  • Recursive Func8ons
  • apparent/real classes, cas8ng, operator

instanceof , func8on equals

  • Abstract classes and methods

I’m gonna assume

you can do this with

your eyes closed by

now

7

What’s in the exam?

  • Everything you needed to know for Prelim I
  • Vector / String class, func8ons
  • Wri8ng func8ons
  • Recursive Func8ons
  • apparent/real classes, cas8ng, operator

instanceof , func8on equals

  • Abstract classes and methods

8

(Fall’07) Question 1 (15 points). Write the body of the

following function recursively.

/** = n, but with its digits reversed.

Precondition: n >= 0.

e.g. n = 135720, value is "027531".

e.g. n = 12345, value is "54321".

e.g. n = 7, value is "7".

e.g. n = 0, value is "0".*/

public static String rev(int n) {

9

returns a String

Recursive Func8on 4 Principles

    1. Write the precise specifica8on

10

/** = n, but with its digits reversed.

Precondition: n >= 0.

e.g. n = 135720, value is "027531".

e.g. n = 12345, value is "54321".

e.g. n = 7, value is "7".

e.g. n = 0, value is "0".*/

public static String rev(int n) {

// base case:

//{n has only one digit}

// recursive case:

// {n has at least two digits}

11

Recursive Func8on 4 Principles

    1. Write the precise specifica8on
    1. Base Case

12

What’s in the exam?

  • Everything you needed to know for Prelim I
  • Vector / String class, func8ons
  • Wri8ng func8ons
  • Recursive Func8ons
  • apparent/real classes, cas8ng, operator

instanceof , func8on equals

  • Abstract classes and methods

19

Administrivia

  • Please remember to fill out your TA evals.
    • Open un8ll Friday 13
    • Good for you , good for us
    • You can give feedback to any TA (not only your lab

instructor)

  • A TA who taught you something
  • A TA that inspired you
  • A TA who you think needs to improve some aspect

20

CS1110 Flix

21

public class Movie {

private String title; // title of movie

private int length; // length in minutes

/** Constructor: document with title t

and len minutes long */

public Movie(String t, int len) {

title= t; length= len;

}

/** = title of this Movie */

public String getTitle()

{ return title; }

/** = length of document, in minutes */

public int getLength()

{ return length; }

/** = the popularity:

shorter means more popular */

public int popularity()

{ return 240 – length; }

}

public class Trailer extends Movie {

/** Constructor: a trailer of movie t.

Trailers are 1 minute long*/

public Trailer(String t)

{super(t, 1);}

}

public class Documentary extends Movie {

private String topic; // …

/** Constructor: instance with title t,

length n, and topic p */

public Documentary(String t, int n,

String p) {

super(t, n);

topic= p;

}

/** = "Documentary" */

public String DocumentaryType()

{ return "Documentary"; }

/** = popularity of this instance */

public int popularity()

{ return 200 - getLength(); }

}

public class Short extends Documentary {

/** Constructor: instance with title t,

length n, and topic p */

public Short(String t, int n, String p)

{ super(t, n, p); }

/** displays acknowledgement */

public String showAck()

{return "We thank our director“;}

/** = "Short Doc" */

public String DocumentaryType()

{ return "Short Doc"; }

}

22

(Fall’05) Ques7on 4 (30 points) For each pair of statements below,

write the value of d aier execu8on. If the statements lead to an error,

write “BAD” and briefly explain the error. (The ques8on con8nues on

the next page.)

Documentary e=

new Short("Man on Wire”, 5, "Bio");

boolean d=

"Short Doc” .equals(e.DocumentaryType());

23

(Fall’05) Ques7on 4 (30 points) For each pair of statements below,

write the value of d aier execu8on. If the statements lead to an error,

write “BAD” and briefly explain the error. (The ques8on con8nues on

the next page.)

Documentary e=

new Short("Man on Wire”, 5, "Bio");

boolean d=

"Short Doc” .equals(e.DocumentaryType());

24

True. method equals here is from the string object

Movie c=

new Documentary(null, 3, "Carter Peace Center");

int d= c.popularity();

25

public class Movie {

private String title; // title of movie

private int length; // length in minutes

/** Constructor: document with title t

and len minutes long */

public Movie(String t, int len) {

title= t; length= len;

}

/** = title of this Movie */

public String getTitle()

{ return title; }

/** = length of document, in minutes */

public int getLength()

{ return length; }

/** = the popularity:

shorter means more popular */

public int popularity()

{ return 240 – length; }

}

public class Trailer extends Movie {

/** Constructor: a trailer of movie t.

Trailers are 1 minute long*/

public Trailer(String t)

{super(t, 1);}

}

public class Documentary extends Movie {

private String topic; // …

/** Constructor: instance with title t,

length n, and topic p */

public Documentary(String t, int n,

String p) {

super(t, n);

topic= p;

}

/** = "Documentary" */

public String DocumentaryType()

{ return "Documentary"; }

/** = popularity of this instance */

public int popularity()

{ return 200 - getLength(); }

}

public class Short extends Documentary {

/** Constructor: instance with title t,

length n, and topic p */

public Short(String t, int n, String p)

{ super(t, n, p); }

/** displays acknowledgement */

public String showAck()

{return "We thank our director“;}

/** = "Short Doc" */

public String DocumentaryType()

{ return "Short Doc"; }

}

26

27

a

Animal

Cat Cat(String, int)

getNoise()

toString()

getWeight()

age

Animal(String, int)

isOlder(Animal)

QUESTION: Which method is called by

Animal t= new Cat(“A”,5); t.toString()?

A. the one in the hidden partition for

Object of a

B. the one in partition Animal of a

C. the one in partition Cat of a

D. None of these

Object

Animal

Cat

the class hierarchy:

Movie c=

new Documentary(null, 3, "Carter Peace Center");

int d= c.popularity();

  • What is the apparent class?
  • Answer: 197. method popularity of

class Documentary is called

Movie

Documentary Trailer

Short

28

Short b= (Short)(new Documentary("", 2, "WMD"));

int d= b.DocumentaryType().length();

29

Short b= (Short)(new Documentary("", 2, "WMD"));

int d= b.DocumentaryType().length();

Movie

Documentary Trailer

Short

  • From documentary, can go (cast) up and back down

to documentary.

  • Think what would happen for the call b.showAck()

30

(Fall’05) Ques7on 4 (24 points). (a) Write an instance method

equals(Object obj) for class Documentary

public class Documentary extends Movie {

/** = "obj is a Documentary with the same values

in its fields as this Documentary" */

public boolean equals(Object obj) {

37

public class Documentary extends Movie {

/** = "obj is a Documentary with the same values

in its fields as this Documentary" */

public boolean equals(Object obj) {

if (!(obj instanceof Documentary) {

38

public class Documentary extends Movie {

/** = "obj is a Documentary with the same values

in its fields as this Documentary" */

public boolean equals(Object obj) {

if (!(obj instanceof Documentary) {

return false;

39

public class Documentary extends Movie {

/** = "obj is a Documentary with the same values

in its fields as this Documentary" */

public boolean equals(Object obj) {

if (!(obj instanceof Documentary) {

return false;

Documentary docObj= (Documentary)obj;

40

Don’t forget to cast.

This is a legal cast. (Why?)

public class Documentary extends Movie {

/** = "obj is a Documentary with the same values

in its fields as this Documentary" */

public boolean equals(Object obj) {

if (!(obj instanceof Documentary) {

return false;

Documentary docObj= (Documentary)obj;

return

getTitle().equals(docObj.getTitle()) &&

getLength() == docObj.getLength() &&

topic.equals(docObj.topic);

41

What’s in the exam?

  • Everything you needed to know for Prelim I
  • Vector / String class, func8ons
  • Wri8ng func8ons
  • Recursive Func8ons
  • apparent/real classes, cas8ng, operator

instanceof , func8on equals

  • Abstract classes and methods

42

Let’s capture the essence of animals

/** representation of an animal */

public class Animal {

private int birthDate; // animal’s birth date

private String predator; // predator of this animal

private String prey; // class of animals this hunts

// move the animal to direction…

public void move(…){

}

// make the animal eat…

public void eat (…){

}

}

43

Problems

  • Animal is an abstract concept
    • Crea8ng an abstract animal doesn’t make sense in the real

world

  • Dogs, cats, snakes, birds, lizards, all of which are animals,

must have a way to eat so as to get energy to move

  • However…
    • Class Animal allows us to create a UFA (uniden8fied flying

animal), i.e. instance of Animal

  • If we extend the class to create a real animal, nothing

prevent us from crea8ng a horse that doesn’t move or eat.

44

Solu8ons

  • How to prevent one from crea8ng a UFA?
    • Make class Animal abstract
      • Class cannot be instan7ated
    • How? Put in keyword abstract
  • How to prevent crea8on paralyzed dogs or starving sharks?
    • Make the methods move and eat abstract
      • Method must be overridden
    • How? Put in keyword abstract and replace the body with ";"

45

Making things abstract

/** representation of an animal */

public abstract class Animal{

private int birthDate; // birth date

private String predator; // animal’s predator

private String prey; // What animal hunts

// Move the animal move in direction …

public abstract void move(…);

// Make the animal eat…

public abstract void eat (…);

46

Good Luck!

47