CS 160 Final Review.

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

1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
18 File handling1June File handling CE : Fundamental Programming Techniques.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Program 6 Any questions?. System.in Does the opposite of System.out.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
CS 160 Final Review. Name 4 primitive type variables?
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CS 160 – Summer 16 Exam 1 Prep.
CSC111 Quick Revision.
File Input / Output.
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Peer Instruction 9 File Input/Output.
I/O Basics.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
An Introduction to Java – Part I
Methods and Parameters
INPUT STATEMENTS GC 201.
CSS161: Fundamentals of Computing
Starting JavaProgramming
Introduction to Classes and Methods
Unit 6 Working with files. Unit 6 Working with files.
Know for Quiz Everything through Last Week and Lab 7
160 Exam 2 Prep.
Introduction to Computing Using Java
File Handling in Java January 19
Building Java Programs
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
F II 6. Using Libraries Objectives
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Java Programming Language
CSC 111 Exam 3 Review Fall 2005.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Peer Instruction 4 Control Loops.
Math class services (functions)
LCC 6310 Computation as an Expressive Medium
Review for Midterm 3.
Lec 17 Using Nested Loops and Objects in an Applet Class
Presentation transcript:

CS 160 Final Review

Name the 8 primitive type variables? int, double, float, long, char, boolean, byte, short

Write the correct contrapositive of the hypothesis O(x) ^ E(y)  E(7xy)? O(7xy)  E(x) v O(y)

Write the correct contradiction of the hypothesis O(x) ^ O(y)  O(7xy)? O(x) ^ O(y) ^ E(7xy)

// Write the correct post-condition public static int foo (int x) { // pre-condition: -3 < x <= 4 return (x * x - 3 * x + 4); } 2 <= return value <= 14

// Write the correct pre-condition public static int foo (int x) { return (x * x - 3 * x - 8); // post-condition: -10 <= return <= 2 } -2 <= x <= 5

What is the loop invariant What is the loop invariant? int x = 2, y = 3, z = v1; while (x <= 4){ z += y; x++; } Since z starts at v1 and 3 gets added 3 times 2 <= x <= 5 y = 3 v1 <= z <= v1 + 9

When x = 3, y = 4, what is the result? x/y + 10

When x = 3, y = 4, what is the result? 20%x + y*3 14

public static char Foo (String s) { return s.charAt(s.length()-1); } 1. What is the return value of this method? 2. What does this method do? 3. How would I call this method in the main? char returns the last character of s Foo(”someString”); or Foo(someStringVariable);

Create a Scanner that reads in a word from the keyboard Create a Scanner that reads in a word from the keyboard. Store the word in a String variable called wordBro. Scanner s = new Scanner (System.in); String wordBro = s.next();

Create a public static method called caster, that returns an int, it takes an double as a parameter. Return the int, caused from typecasting the parameter. public static int caster(double d){ return (int) d; }

Create a Scanner that reads in an integer, double and then a full line from a file called filename. Store into my PRE-DEFINED variables: myInt, myDouble, myLine. try{ Scanner s = new Scanner(new File(“filename”)); myInt = s.nextInt(); myDouble = s.nextDouble(); s.nextLine(); myLine = s.nextLine(); s.close(); } catch (IOException e) { System.out.println(“Error: could not open file: “ + filename); } OR Scanner s = null; s = new Scanner(new File(“filename”));

Declare and assign a 3x3 2D double array, called doubleTable, with all of the values assigned to 2.0. double [] [] doubleTable = new double [3][3]; for (int i = 0; i < doubleTable.length; i++) for (int j = 0; j < doubleTable[i].length; j++) doubleTable[i][j] = 2.0; OR double [] [] doubleTable = {{2.0, 2.0, 2.0},{2.0, 2.0,2.0},{2.0,2.0,2.0}};

public class StudentData {. int id;. String firstName, lastName; public class StudentData { int id; String firstName, lastName; public StudentData ( int id, String _firstName, String _lastName) { this.id = id; firstName = _firstName; lastName = _lastName; } } Initialize an array of StudentData objects called “CS160”. The array is of size 3. StudentData [] CS160 = new StudentData [3];

public class StudentData {. int id;. String firstName, lastName; public class StudentData { int id; String firstName, lastName; public StudentData ( int _id, String _firstName, String _lastName) { id = _id; firstName = _firstName; lastName = _lastName; } } Use the following information to create three StudentData objects and store into the CS160 array from Part A. - 1 Rob Drobs - 2 Bob Crobs - 3 Steve Jobs CS160[0] = new StudentData (1, “Rob”, “Drobs”); CS160[1] = new StudentData(2, “Bob”, “Crobs”); CS160[2] = new StudentData(3, “Steve”, “Jobs”);

What does the following code do/What is array_name assigned to after this code is done? int [] [] array_name = new int [4][7]; int i = 1; for (int row = 0; row < array_name.length; row++) for (int col = 0; col < array_name[row].length; col++){ array_name[row][col] = i; i++; } prints the February calendar 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

Make a Scanner called reader, that reads a file called “names.txt”. try{ Scanner reader = new Scanner(new File(“names.txt”)); } catch (IOException e) { System.out.println(“Error: could not open file: names.txt”); }

Make a constructor for the following class Make a constructor for the following class. This constructor assigns the number and exponent instance variables using the variables passed into the method and assigns the instance variable called result to the number raised to the exponent. public class Power { private int number; private int exponent; private int result; // constructor goes here } public Power (int n, int e) { number = n; exponent = e; result = Math.pow (n, e); }

public class CoolName { public static int [] array = {1, 2, 3, 4, 5}; public void multiplier (int i) { for (int i = 0; i< array.length; i++) array[i] *= d; } public static void main (String [] args){ // code here System.out.println(Arrays.toString(array)); - How would I call the following method? - What is the output when d is 3? CoolName c = new CoolName(); c.multiplier (3); [3, 6, 9, 12, 15]

- Will this print to the file called “input.txt”? public class ClassCoolName { PrintWriter pw = null; try { pw = new PrintWriter (“input.txt”); } catch (IOException e) { System.out.println(e); } System.out.println(“I printed!!”); pw.close(); - Will this print to the file called “input.txt”? No, you need to instead do pw.println(“I printed!!”); to print to the file, currently this prints to the console.

- Will this print to the file called “input.txt”? public class ClassCoolName { PrintWriter pw = null; try { pw = new PrintWriter (“input.txt”); } catch (IOException e) { System.out.println(e); } pw.println(“hi”); - Will this print to the file called “input.txt”? No, the PrintWriter never got closed so it never got printed to the file.

What does this print. String s = “marco polo”; System. out. println(s What does this print? String s = “marco polo”; System.out.println(s.substring(0,3)); mar

Would this correctly read in the file, given my file is “input Would this correctly read in the file, given my file is “input.txt” and it contains: Hi there How are you? Scanner read = new Scanner (”input.txt”); String s = read.next(); String s1 = read.next(); read.nextLine(); String s2 = read.nextLine(); No, I didn’t add ”new File” before my input.txt. It should be Scanner read = new Scanner (new File (“input.txt”)); How the code is written the Scanner will parse the String “input.txt”

What is the loop invariant What is the loop invariant? int x = 1, y = 2, z = -5; while (x <= 5){ z +=y; x++; } y = 2, 1 <= x <= 6, =5 <= z <= 5 y = 2, 1 <= x <= 6, z = -5 + 5y (better solution)

Write a for loop that prints the reverse of the String variable s. for (int i = s.length() -1; i >= 0; i--) System.out.print(s.charAt(i));

Write a constructor for this following class Write a constructor for this following class. This constructor assigns the two instance variables (name and breed) to two variables from the constructors parameters. public class Dog { private String name, breed; // constructor goes here } public Dog (String n, String b) { name = n; breed = b; }

Declare a Scanner that reads from the keyboard Declare a Scanner that reads from the keyboard. Write a do-while loop that asks the user “Are we there yet?”, give the user instructions (for example “yes will stop this loop”), store their response into a String variable, and stop the loop when the word is “yes”. NOTE: you can assume that the user will only enter a one word response. Scanner keys = new Scanner (System.in); String word = ""; do { System.out.println("Are we there yet?"); System.out.println("Enter yes to stop"); word = keys.next(); } while (!word.equalsIgnoreCase("yes"));

What are the following errors (there are 2) What are the following errors (there are 2)? public void writeFile (String filename){ try { PrintWriter pw = new File (filename); for (int i = 0; i < 100; i++) pw.printf(“%d”, i); } catch (Exception e) { System.out.println(“Can’t write to “ + filename); } didn’t close pw needs to be (no new PrintWriter): PrintWriter pw = new PrintWriter (new File (filename)); or PrintWriter pw = new PrintWriter (filename);

What does this code do? public void Foo (int [] array, char a, char b){ for (int i = 0; i < array.length/2; i++) array[i] = a; for (int i = array.length/2; i < array.length; i++) array[i] = b; } makes the first half of array to be the ascii value of the a variable and the second half of the array to be the ascii value from the b variable.

Let’s talk…

How do you compare objects and Strings? .equals, NOT ==

What is the difference between Pass-by-Value and Pass-by-Reference What is the difference between Pass-by-Value and Pass-by-Reference? What are some examples of each? Pass-by-value : primitive types (int, double, char, etc). Must reassign to change a pass-by-value variable. Pass-by-reference: objects (ie Arrays or objects that you create (ie CS160 object Array, as well as all of the objects inside of the CS160 object Array))

Why do you need to put a .nextLine() after you use .next(), .nextInt(), .nextDouble(); ? token processing to line processing. .nextLine looks for a new line character (’\n’) in a line, so after reading a word, int, double, etc there is still a ‘\n’ character to read, so you must “eat” the rest of the line to read the next line.

Why do we need toString methods. What do toStrings do Why do we need toString methods? What do toStrings do? Do they return anything, if so what? Overrides default, lets us print out objects, otherwise we would only print their memory address. They return Strings, they DON’T PRINT ANYTHING!!