A Few Review Questions Dan Fleck CS211 Fall 2007.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

Access to Names Namespaces, Scopes, Access privileges.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 2: Java Fundamentals
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Comparison-Based Sorting & Analysis Smt Genap
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs
Introduction to Recursion
Introduction to Recursion
Stacks and Queues.
Exceptions: When things go wrong
Chapter 10 – Exception Handling
“Form Ever Follows Function” Louis Henri Sullivan
CS216: Program and Data Representation
Lecture 2: Data Types, Variables, Operators, and Expressions
Exceptions, Interfaces & Generics
Programming Fundamentals Lecture #7 Functions
Namespaces, Scopes, Access privileges
Stacks and Queues.
Building Java Programs Chapter 14
Stack Data Structure, Reverse Polish Notation, Homework 7
Building Java Programs
Stacks and Queues.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs Chapter 7
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Stacks and Queues.
Conditional Statements
Introduction to Java Programming
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Building Java Programs
Building Java Programs
Building Java Programs
Byte Code Verification
Stacks and Queues CLRS, Section 10.1.
Stacks Abstract Data Types (ADTs) Stacks
Namespaces, Scopes, Access privileges
Basics of Recursion Programming with Recursion
slides created by Marty Stepp
Constructors, GUI’s(Using Swing) and ActionListner
Peer Instruction 4 Control Loops.
Building Java Programs
Building Java Programs
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Introduction to java Part I By Shenglan Zhang.
Corresponds with Chapter 5
Stacks and Queues.
Presentation transcript:

A Few Review Questions Dan Fleck CS211 Fall 2007

Sort using Radix Sort: What is the algorithm? Group by least significant digit, maintaining order in the groups Group by next least significant digit maintaining order in the groups Repeat until you have no more digits Note: If a digit doesn’t exist, move to the front of the list

Sort using Radix Sort: 103 227 357 897 34 1009 208 103 34 227 357 897 208 1009 103 208 1009 227 34 357 897 34 1009 103 208 227 357 897 Wed we got here (Fall 2007) 34 103 208 227 357 897 1009

Insertion Sort What is the algorithm? Partition the array into two regions, sorted and unsorted. While unsorted values remain, copy the unsorted value and insert it into the array in the correct order (shifting array values as required)

Insertion Sort 10 7 6 8 2 12 8 7 10 6 8 2 12 8 6 7 10 8 2 12 8 6 7 8 10 2 12 8 2 6 7 8 10 12 8 2 6 7 8 10 12 8 2 6 7 8 8 10 12

What is wrong with this? What are two ways to fix it? The following class is located in directory: /usr/dfleck/javacode/debug/src/ package edu.gmu.test; public class MyTest { public int add(int a, int b) { return a+b; }

To make a Jar executable what does it need to know? Answer: The class to run when it is executed. This is done by setting the main-class attribute in the MANIFEST.MF file in the jar. How do I get a list of files in a jar?

Three compiler errors are present with this code. What are they? public class MyTest { public int add(int a, int b) { return a+b; } public static void main(String []args) { int v1 = args[0]; int v2 = args[1]; System.out.printf(“the sum of %d and %d is = %d”, v1, v2, add(v1, v2));

Three compiler errors are present with this code. What are they? public class MyTest { public static int add(int a, int b) { return a+b; } public static void main(String []args) { int v1 = Integer.parseInt(args[0]); int v2 = Integer.parseInt(args[1]); System.out.printf(“the sum of %d and %d is = %d”, v1, v2, add(v1, v2));

Rewrite this in java Notes: // 0 0:ldc1 #10 <String "hello"> // 1 2:astore_1 // 2 3:aload_1 // 3 4:areturn Notes: ldc1 -- load a constant onto the stack (String constant in this example) astore -- pop the stack and store the object (reference) as a local var X aload -- push an object (reference) onto the stack areturn -- return from this method the object on top of the stack

Rewrite this in java public String getString() { return “Hello”; } // 0 0:ldc1 #10 <String "hello"> // 1 2:astore_1 // 2 3:aload_1 // 3 4:areturn public String getString() { return “Hello”; }

public class MyClass { static String course=“CS211”; String prof = “Fleck”; private void updateCourse() { course = “CS310”; prof = “Nordstrum”; } public void main(String []args) { MyClass a = new MyClass(); MyClass b = new MyClass(); System.out.println(“A Crs is: %s Prof is: %s”,a.course,a.prof); b.updateCourse(); System.out.println(“B Crs is: %s Prof is: %s”,b.course,b.prof);

public class MyClass { static String course=“CS211”; String prof = “Fleck”; private void updateCourse() { course = “CS310”; prof = “Nordstrum”; } public void main(String []args) { MyClass a = new MyClass(); MyClass b = new MyClass(); System.out.printf(“\n A Crs is: %s Prof is: %s”,a.course,a.prof); b.updateCourse(); System.out.printf(“\n B Crs is: %s Prof is: %s”,b.course,b.prof); A Crs is: CS211 Prof is:Fleck A Crs is: CS310 Prof is:Fleck B Crs is: CS310 Prof is:Nordstrum

Public class A { public void aMethod() { /* something */ } } Public class B extends A { public void bMethod() { /* something */ } } Which if any are valid? A a = new A(); B b = new B(); A cb = new B(); a.aMethod(); a.bMethod(); b.aMethod(); b.bMethod(); cb.aMethod(); cb.bMethod();

Public class A { public void aMethod() { /* something */ } } Public class B extends A { public void bMethod() { /* something */ } } Which if any are valid? A a = new A(); B b = new B(); A cb = new B(); a.aMethod(); a.bMethod(); b.aMethod(); b.bMethod(); cb.aMethod(); cb.bMethod();

2. (2 points) Given this interface: public interface CanHear { 2. (2 points) Given this interface: public interface CanHear { public void listen(int volume); public boolean heardSomething(); public int getNumberOfEars();} Fix the AlwaysHears class by writing the code necessary for it to compile. /** The AlwaysHears class ALWAYS hears something. * If someone asks if it heard something, it will always return true. */ public class AlwaysHears implements CanHear { public int numberOfEars; public AlwaysHears(int numberOfEars) { this.numberOfEars = numberOfEars; } public void listen(int volume) { System.out.println( "I'm listening at volume :"+volume); }

public float divide(. float numerator, public float divide( float numerator, float denominator) { if (denominator == 0) { throw new Exception( "You cannot divide by zero!"); } float returnValue = numerator/denominator; return returnValue; } Does this compile? If not, give me two ways to fix it.

During debugging why do you look at the call stack During debugging why do you look at the call stack? (and what is the call stack?) The call stack is the list of methods that were called to get to the current location in the code. You look at it to confirm you got to this point in the code by calling the methods you expected

Can you: Write a recursive method that takes a parameter n and will return the sum of integers 1 through n. For example, given n=3 it should return 6 (3+2+1). This code should work for any value of n >= 1.

Always remember… these review slides are NOT complete Always remember… these review slides are NOT complete! You must look at the study plan on the class website for a more thorough plan!