First Semester 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

10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Review Java.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
int [] scores = new int [10];
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Java Primer 1: Types, Classes and Operators
Arrays 2/4 By Pius Nyaanga.
University of Central Florida COP 3330 Object Oriented Programming
Chapter 6 More Conditionals and Loops
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
Building Java Programs
Building Java Programs
An Introduction to Java – Part I
CSC 142 Computer Science II
Control Statement Examples
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.
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
An Introduction to Java – Part I, language basics
Building Java Programs
Chapter 2: Basic Elements of Java
Week 6 CS 302 Jim Williams, PhD.
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.
Review for Final Exam.
Building Java Programs
CHAPTER 6 GENERAL-PURPOSE METHODS
Exam 1 Material Study Guide
Building Java Programs
Object Oriented Programming in java
Building Java Programs
python.reset() Also moving to a more reasonable room (CSE 403)
CS 200 Primitives and Expressions
int [] scores = new int [10];
Lecture 10: Arrays AP Computer Science Principles
Arrays.
Code Refresher Test #1 Topics:
Review for Final Exam.
Why did the programmer quit his job?
Suggested self-checks: Section 7.11 #1-11
CIS 199 Final Review.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Fundamental Programming
In this class, we will cover:
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Just Enough Java 17-May-19.
slides created by Ethan Apter and Marty Stepp
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

First Semester Review

List of Topics Covered OOP Concepts Objects, methods, classes Documentation Identifiers Method calls Target, method, arguments The Java API String methods Math methods Arithmetic operations +, -, *, / , % Compound assignment/increment-decrement Primitive types Conversion/casting Variables Declaration, assignment, initialization References (objects) v. primitives Arrays Declaring, initializing Accessing elements Traversal Multi-dimensional ArrayList Methods Declaring, calling Parameters Reference Semantics Return values Overloading Loops while, do-while, for Conditionals if, if-else, if-else if-else Relational operators Boolean operators Console input/Scanner Classes Constructors Fields Methods Access (public/private) Constants (final)

Primitive Types Which of the following statements displays 1234? (There may be more than one.) System.out.print(12 * 100 + 34); System.out.print(“12” + 34); System.out.print(12 + “34”); Answer: I, II, and III The + operator defaults to arithmetic addition, unless one of the operands is a string, in which case it is concatenation.

Arithmetic Operators Consider the following method: public static int countEvenDigits(int n) { int count = 0; while (n > 0) { int d = n % 10; if (d % 2 == 0) { count++; } <statement1> return count; What should go in place of <statement1> to make the method work as intended? Answer: n /= 10;

Overloading True or false: Overloaded methods may have the same number of parameters? Answer: True As long as the types of the parameters are different, two overloads may have the same number of parameters.

Parameter Passing Consider the following method: public void change(double[] nums) { for (int k = 0; k < nums.length; k++) { nums[k] = 5.4; } What will be stored in samples after the following code is executed? double[] samples = {1.0, 2.1, 3.2, 4.3}; change(samples); Answer: {5.4, 5.4, 5.4, 5.4} The array is passed by value, but changing the elements still modifies the original array.

Two-Dimensional Arrays What should go in the place of <expression1> and <expression2> to make the following correctly populate a two-dimensional array vals with random numbers between 0 and 1. for (int i = 0; i < vals.length; i++) { for (int j = 0; j < <expression1>; j++) { <expression2> = Math.random(); } Answer: vals[i].length and vals[i][j]

Boolean Logic What is the value of the following expression when a is false and b is true? !(!a || b) || (!a && b) Answer: true a = true a = false b = true false true b = false

Debugging Find a bug in the following method: public static double volumeOfSphere(int r) { double pi = 3.14159; return 4 / 3 * pi * Math.pow(r, 3); } Answer: The expression (4 / 3) will have the value 1 because it is integer division.

Random Numbers Write an expression to generate a random integer between 2 and 50 inclusive. Answer: (int)(Math.random() * 49) + 2;

Constructors True or false: Constructors’ return type must be declared as void. Answer: False Constructors do not have a declared return type.

Console Output What is printed by the following code: public static void printStuff() { System.out.print(“Your rating: ”); for (int i = 0; i < 3; i++) { System.out.println(“*”); } System.out.println(“\nGood job!”); Answer: Your rating: * * Good job!

Loops How many lines of output will this code produce? Answer: 6 for (int n = 50; n > 0; n /= 2) { System.out.println(n); } Answer: 6

Loops/Arrays Give a brief description of what the following code does. // a is an initialized array of integers int c = 0; int[] a2 = new int[a.length]; for (int i = 0; i < a.length; i++) { if (a[i] >= 0) { a2[c] = a[i]; c++; } Answer: Copy all non-negative integers from a to a2.

String Methods What is returned when the following method is called with arguments “cat”, “xy”, and 1? public static String insert(String str1, String str2, int pos) { String first = str1.substring(0, pos); String second = str1.substring(pos); return first + str2 + second; } Answer: “cxyat”

Arrays What will happen when the following code is executed? int[] values = {5, 6, 3, 7, 1, 4, 9, 8, 0, 7, 12}; int[] counts = new int[10]; // initialize counts to all zeroes for (int i = 0; i < values.length; i++) { counts[values[i]]++; } Answer: An ArrayIndexOutOfBoundsException will be thrown. 12 is an invalid index for counts

Defining Classes Consider the following incomplete class: Answer: public class Student { private String name; /* more code */ } Write an accessor method for the name variable in the Student class. Answer: public String getName() { return name;

Constructors Consider the following class: Public class Card { private String suit; private int value; public Card(String suit, int value) { ... } } Write an implementation for the constructor. Answer: public Card(String suit, int value) { this.suit = suit; this.value = value;

Final Question

Final Question Implement the following method: // Given a square array of ints, determine whether // or not the sums of the integers on the two // diagonals are equal public static boolean areDiagsEqual(int[][] vals)

Final Question Implement the following method: public static boolean areDiagsEqual(int[][] vals) { int diag1 = 0, diag2 = 0; int n = vals.length – 1; for (int i = 0; i < vals.length; i++) { diag1 += vals[i][i]; diag2 += vals[i][n – i]; } return diag1 == diag2;