CSC 111 Exam 3 Review Fall 2005.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Written by: Dr. JJ Shepherd
CS 112 Intro to Computer Science II Sami Rollins Fall 2006.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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;
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Data Types & Operations 1 Last Edited 1/10/04CPS4: Java for Video Games Data Types.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Agenda Object Oriented Programming Reading: Chapter 14.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Counting Coins. The Basics Quarter 25 cents Dime 10 cents.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Looping and Counting Lecture 3 Hartmut Kaiser
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
OOP Basics Classes & Methods (c) IDMS/SQL News
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Objects Real and Java COMP T1 3
Chapter 2 Basic Computation
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Console Output, Variables, Literals, and Introduction to Type
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 4: Writing Classes
Chapter 2 Basic Computation
INPUT STATEMENTS GC 201.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
IDENTIFIERS CSC 111.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Two big words Inheritance Polymorphism
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Building Java Programs
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Suggested self-checks: Section 7.11 #1-11
Review of Previous Lesson
Review of Previous Lesson
Presentation transcript:

CSC 111 Exam 3 Review Fall 2005

Classes – Construction Behaviors Here’s a class hierarchy A Vehicle has the following attributes: String manufacturer String make int year int numberOfWheels double engineSizeInLiters Vehicle Car Truck SportsCar

Classes – Construction Behaviors Write a constructor for the Vehicle class that takes parameters that set all 5 attributes Write a constructor for Vehicle that takes all parameters except year. It should by default set year equal to the current year. Vehicle Car Truck SportsCar

Classes – Construction Behaviors Vehicle Car Truck SportsCar

Classes – Construction Behaviors A Car is a Vehicle which has the following constraints: Must have 4 wheels Has an additional double attribute, called trunkSize Write the fiver parameter constructor (manufacturer, make, year, engineSizeInLiters, trunkSize) for Car

Classes – Construction Behaviors Write the fiver parameter constructor (manufacturer, make, year, engineSizeInLiters, trunkSize) for Car

Classes On the following slides, when I call the construction behavior Car c = new Car(); I actually intend it to be that I’m sending in all 5 parameters – I’m running out of space so I left the parameters out. A real use of the Car constructor would look like: Car c = new Car(“Chevrolet”, “Camaro”, 1995, 5.7, 9.0);

Classes Given the hierarchy to the right, which of the following instructions are valid? Vehicle Vehicle v = new Vehicle(); Vehicle v = new Car(); Car c = new Vehicle(); Car c = new SportsCar(); Car c = new Truck(); Vehicle v = new Truck(); SportsCar sc = new SportsCar(); Truck t = new Truck(); Car Truck SportsCar

Classes Given the hierarchy to the right, which of the following instructions are valid? Vehicle Car Truck VALID Vehicle v = new Vehicle(); VALID Vehicle v = new Car(); INVALID Car c = new Vehicle(); VALID Car c = new SportsCar(); INVALID Car c = new Truck(); VALID Vehicle v = new Truck(); VALID SportsCar sc = new SportsCar(); VALID Truck t = new Truck(); SportsCar

Classes Vehicle Given the hierarchy and instructions to the right, what would be the answer to the following instance of questions? Car Truck SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar(); Is v1 an instanceof Car? Is v2 an instance of Car? Is v3 an instance of Car? Is c an instance of Car? Is sc an instance of Car? Is c an instance of SportsCar? List all classes v3 is an instanceof. List all variables that are List all classes v2 is an instanceof. an instanceof Car

Classes Vehicle Car Truck Given the hierarchy and instructions to the right, what would be the answer to the following instance of questions? SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar(); Is v1 an instanceof Car? FALSE Is v2 an instance of Car? TRUE Is v3 an instance of Car? FALSE Is c an instance of Car? TRUE Is sc an instance of Car? TRUE Is c an instance of SportsCar? TRUE List all classes v3 is an instanceof. Truck, Vehicle List all classes v2 is an instanceof. Car, Vehicle List all variables instanceof Car: v2, c, sc.

Classes Vehicle Assume all class definitions have get and set methods for the attributes they define (like getMake() in Vehicle, getTrunkSize() in Car …) Given the hierarchy and instructions to the right, write a public static void method called printManufacturer that could take any of the variables and print out their “manufacturer”. public static void printManufacturer(??) { ?? } Car Truck SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar();

Classes Vehicle Given the hierarchy and instructions to the right, write a public static void method that could take any of the variables and print out their “manufacturer”. Assume all class definitions have get and set methods for their attributes. public static void printManufacturer(Vehicle vehicle) { System.out.println( vehicle.getManufacturer()); } Car Truck SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar();

Classes Vehicle Assume that there is an abstract behavior called double computeFuelEfficiency() in the Vehicle class. Assume that method is implemented in Car and Truck. Which of the following statements are valid? Car Truck SportsCar Vehicle v = new Vehicle(); System.out.println(v.computeFuelEfficiency()); Car c = new Car(); System.out.println(c.computeFuelEfficiency()); Vehicle v2 = new Car(); System.out.println(v2.computeFuelEfficiency()); SportsCar sc = new SportsCar(); System.out.println(sc.computeFuelEfficiency());

Classes Vehicle v = new Vehicle(); System.out.println(v.computeFuelEfficiency()); ABOVE IS ILLEGAL – CAN’T CONSTRUCT VEHICLES (= new Vehicle()) BECAUSE ABSTRACT CLASS Car c = new Car(); System.out.println(c.computeFuelEfficiency()); TWO ABOVE ARE LEGAL Vehicle v2 = new Car(); System.out.println(v2.computeFuelEfficiency()); TWO ABOVE ARE LEGAL (You’re really a Car, Vehicles have it listed as callable method) SportsCar sc = new SportsCar(); System.out.println(sc.computeFuelEfficiency());

Classes Inheritance or dependency? Library and Book Student and Course Politician and Governor Currency and Euro Ship and Aircraft Carrier Ship and Fleet

Classes Inheritance or dependency? Library and Book Dependency Student and Course Dependency Politician and Governor Inheritance Currency and Euros Inheritance Ship and Aircraft Carrier Inheritance Ship and Fleet Dependency

File Input To the right is the start of a simple encryption program. It already has a reverse static method which takes as a single parameter a String to reverse and that returns the reversed String, as well as code in main to ask the user what file to encrypt. Finish the program using file input instructions so that it 1) opens the requested file for reading, 2) reads and reverses each line in the file and prints the reversed lines to the screen, and 3) at the end closes the file.

File Input To the right is the start of a simple encryption program. It already has a reverse static method which takes as a single parameter a String to reverse and that returns the reversed String, as well as code in main to ask the user what file to encrypt. Finish the program using file input instructions so that it 1) opens the requested file for reading, 2) reads and reverses each line in the file and prints the reversed lines to the screen, and 3) at the end closes the file.

File Output Test Program Output File

File Output A Student class has the following attributes: String firstName String lastName String major int idNumber Write a saveToFile Behavior that Has no return type and a String parameter which is the filename to write to Opens the file specified by filename for writing Inside the file, write the data in this format lastName, firstName id major Make sure all items written are really written to the file and close the file using the appropriate behaviors

File Output A Student class has the following attributes: String firstName String lastName String major int idNumber Write a saveToFileBehavior that Has no return type and a String parameter which is the filename to write to Open the file for writing Inside the file, write the data in this format lastName, firstName id major Make sure all items written are really written to the file and close the file using the appropriate behaviors

File Input/Output Review HourlyEmployee constructor for constructing objects from a file.

Arrays - Creating Write a Java instruction to create an array that could hold all 26 characters in the alphabet. Name the array whatever you would like. The primitive type for characters is char. Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. There are eight types of currency you will hold in the drawer ($20, $10, $5, $1, quarters, nickels, dimes, and pennies). Remember that you can’t hold 1.5 quarters (they don’t break in half that easily!) Write a Java instruction to create an array for this problem. Name the array register. What would be the appropriate type for the array and how big would it need to be?

Arrays - Creating Write a Java instruction to create an array that could hold all lowercase characters in the alphabet. Name the array whatever you would like. The primitive type for characters is char. char[] alphabet = new char[26]; Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. These are the types of currency you will hold in the drawer ($20 bills, $10 bills, $5 bills, $1 bills, quarters, nickels, dimes, and pennies). Remember that you can’t hold 1.5 quarters (they don’t break that easily!) Write a Java instruction to create an array for this problem. Name the array register. What would be the appropriate type for the array and how big would it need to be? int[] register = new int[8];

Arrays - Creating Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. There are eight types of currency you will hold in the drawer ($20, $10, $5, $1, quarters, nickels, dimes, and pennies). Assume that the currency counts are held in the order listed above in your register array (register[0] is the number of 20’s, register[1] is the number of 10’s, … register[4] is the number of quarters, register[7] is the number of pennies). Write a (set of?) Java instruction(s) that indicates your register drawer starts with 5 each of 20, 10, 5, and 1 dollar bills and 10 each of quarters, nickels, dimes, and pennies.

Arrays – Creating/Writing Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. There are eight types of currency you will hold in the drawer ($20, $10, $5, $1, quarters, nickels, dimes, and pennies). Assume that the currency counts are held in the order listed above in your register array (register[0] is the number of 20’s, register[1] is the number of 10’s, register[4] is the number of quarters, register[7] is the number of pennies). Write a (set of?) Java instructions that indicates your register drawer starts with 5 each of 20, 10,5, and 1 dollar bills and 10 each of quarters, nickels, dimes, and pennies. int[] register = {5,5,5,5,10,10,10,10}; OR int[] register = new int[8]; register[0] = 5; register[1] = 5; register[2] = 5; register[3] = 5; register[4] = 10; register[5] = 10; register[6] = 10; register[7] = 10;

Arrays - Reading Write a set of instructions that computes how many total pieces of currency (how many coins and bills) are in the register? Your answer should be stored in the integer variable totalCurrencyCount.

Arrays – Reading - Answer Write a set of instructions that computes how many total pieces of currency (how many coins and bills) are in the register? Your answer should be stored in the integer variable totalCurrencyCount.

Arrays – Reading What if you just wanted to count the number of coins in the drawer? Your answer should be stored in the integer variable totalCoinCount.

Arrays – Reading What if you just wanted to count the number of coins in the drawer? Your answer should be stored in the integer variable totalCoinCount.

Arrays Writing What would the array contents of the char array slogan2 be after the following array reading/writing instructions?

Arrays Writing What would the array contents be after the following array reading/writing instructions?