Download presentation
Presentation is loading. Please wait.
1
CS Week 3 Jim Williams, PhD
2
Effort Influences Intelligence
"...effort and difficulty, that's when their neurons are making new connections, stronger connections. That's when they're getting smarter." - Carol Dweck
3
This Week Exam Confirmation and Accommodation requests due by Friday
Install Eclipse Team Lab: (Tue/Wed): Program 2 (due Thurs): Lecture: Review Methods, Using Objects
4
Calling Class (static) Methods
double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int num1 = 16; int num2 = 3; double result; result = num2 + Math.sqrt( num1);
5
Is this mPrint Call or Definition?
static void mPrint() { System.out.println("my print"); } B is the correct answer
6
Defining Methods public class M { //method definition
static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); // method call. B is the correct answer
7
Is count: Argument or Parameter?
public static void main(String []args) { int num = 10; printCount( 23); printCount( num+3); } static void printCount(int count) { System.out.println( count); argument parameter count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).
8
Argument vs. Parameter static void printCount(int count) { //parameter
public static void main(String []args) { int num = 10; printCount( 23); //argument is value passed in printCount( num+3); } static void printCount(int count) { //parameter System.out.println( count); count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).
9
What prints out? static void calc(int num) { num = 3; }
5 35 error static void calc(int num) { num = 3; } public static void main(String []args) { int n = 5; calc( n); System.out.println( n); try it.
10
Which is called first: calc or println?
error static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); put a print statement within the calc method to see if it is called before the println method.
11
Returning a Value from a Method
static int triple(int num) { return num * 3; } public static void main(String []args) { int value = 5; int result = triple( value);
12
Instance vs. Class (static) Methods
method definition has “static” modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have “static” modifier use instance of class when calling Random randGen = new Random(); randGen.nextInt( 5);
13
Primitive vs Reference Data Types
Store value int i = 5; Reference Store a reference to another location in memory that contains value Integer num; num = new Integer(9);
14
Primitive vs Reference Variables
int num1; num1 = 5; Integer num2; num2 = new Integer(7); Integer num3; num3 = 9;
15
Will this work? Double d = new Double(10); double d2 = d; yes no
sometimes error yes. try it. The second line is an example of implicit unboxing.
16
String String name; name = "Alex";
17
Calling String methods
aChar: 'i' aChar: 's' aChar: ' ' String strA = "This is a string"; char aChar = strA.charAt( 3);
18
What is the answer? AP? String s1 = "An important programming tool.";
String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase(); AP? api ANP IM API try it.
19
What kind of methods? max: class nextInt: instance Math.max( 10, 20);
max: instance nextInt: class Math.max( 10, 20); Random randGen = new Random(); randGen.nextInt( 5); class methods (static) instance methods (non-static) A is the best answer
20
java.util.Random Random randGen; //Declare reference variable
randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);
21
Read In Values Recall: import java.util.Scanner;
Scanner input = new Scanner( System.in); int age = input.nextInt(); String name = input.nextLine();
22
Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n");
int a = input.nextInt(); if ( input.hasNextInt()) { int b = input.nextInt(); } else { input.nextLine(); } String line = input.nextLine(); System.out.println("#" + line + "#");
23
What are values of variables?
name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: name: Minsub age: 22 String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();
24
What are values of variables?
name: Minsub\n22\CS age: major: name: Minsub age: 22 name: Minsub major: CS String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();
25
What are values of variables?
score1: 20 score2: 25 title: scores title: score1: score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();
26
Scanner num: 0 str4: 3 str4: line note. num: 3 str4:
String note = "Hmm\na \na\n3\n\nline note."; Scanner input = new Scanner( note); int num = 0; String str1 = input.nextLine(); String str2 = input.next(); if ( input.hasNextInt()) { num = input.nextInt(); } String str4 = input.nextLine();
27
Debugging with Print statements
See what is going on. Divide and conquer.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.