Download presentation
Presentation is loading. Please wait.
1
CS Week 2 Jim Williams, PhD
2
Society of Women Engineers
"Not just for women and not just for engineers; all students are welcome. We are especially looking for more computer science students!" - Emile Wille (Marketing Officer for SWE)
3
This Week Team Lab: Tuesday or Wednesday Program 1: Due Thursday
Lecture: Primitive Data Types and Expressions
4
Team Labs First meeting this week. 1350 cs and 1370 cs Meet TAs & LAs
1st floor, around corner from elevators Meet TAs & LAs Work in small groups and pairs Discuss Terms, Trace & Explain code Edit-Compile-Run Cycle on the command-line
5
Pair Programming 2 people working together on 1 computer.
One person types, the other provides direction and reviews. Many report more confidence in solution and more enjoyment programming. Important to switch roles (who has the keyboard). Provide respectful, honest and friendly feedback.
6
Learning to Trace with Java Visualizer
Java Visualizer available via our course website or: import java.util.Scanner; public class RectangleArea { public static void main(String [] args) { Scanner input = new Scanner(System.in); int height = 0; int width = 0; System.out.print("Enter height: "); height = input.nextInt(); System.out.print("Enter width: "); width = input.nextInt(); int area = height * width; System.out.println("A rectangle with height " + height + " and width " + width + " has an area of: " + area); input.close(); return; //optional at end of method, unless returning value }
7
Programming Process Users Files Programmer Editor (Virtual) Compiler
Machine Compiler Hello.java Hello.class Computer Files Programmer
8
Programming Errors Naming/Saving Syntax/Compile time Runtime & Logic
Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer
9
Edit-Compile-Run Cycle
From the command-line: notepad javac java
10
Programming Process Question
Cat.txt Cat.java Cat.class Cat For the class named Cat, what is the name of the file that the compiler creates and is provided to the Java Virtual Machine to run?
11
Learning Programming Lots of little steps that quickly build
Not mastering little steps creates bigger steps Goes beyond Remembering and Understanding to Applying, Analyzing, Evaluating and Creating. Goal: Have basic fluency in Java and be able to solve small problems using it.
12
Primitive Data Types int whole numbers (4 bytes)
double floating point numbers (8 bytes) char single character (2 bytes) boolean true or false value (>= 1 bit) less commonly used: float (4), long (8), short (2), byte (1)
13
Primitive Data Type Conversion
Which work? double a = 5; double b = 6.0; int c = 3.0F; float d = b; c = d; d = c; char e = 'A'; int f = e; short s = f; int h = / 3; int i = 10.0 / 3; int j = (int) 10.0 / 3; double k = (int) 10.0 / 3.0
14
Operator Precedence & Associativity
15
Constant public class World { public static void main(String []args) {
int size = 3; final int MAX_SIZE = 10; }
16
String class A reference (class), not a primitive data type.
Frequently used final String TITLE_PREFIX = "Welcome to "; int week = 2; String welcome = "Week " + week; System.out.println( TITLE_PREFIX + welcome);
17
What are the values in a, b & c?
int a = 5; int b = 7; int c = a; b = c; a = b;
18
What are the values in a, b & c?
int a = 2; int b = 1; int c = a + b; a = b; a = c;
19
Swap values in a & b int a = 5, b = 3;
Write code to swap values in a & b. a = b; b = a; c = b; b = c; c = a; a = c; Google "swapping values without third variable" for many creative ways.
20
Application: Temperature Conversion
(Degrees Fahrenheit – 32) x 5 / 9 = Degrees Celsius What symbols have different meanings in Java? What changes must be made to implement this equation in Java? Retrieval practice importance of committing to an answer
21
My List X vs * equals (==) vs assignment (=)
value is stored on the left hand side of assignment (=) operator Variables: name areas of computer memory, declare before use, declare type of data, initialize Variable names: start with letter, include letters numbers and _, but no spaces Conventions: camelCasing, spell out names Semicolon at the end of statements
22
CS Core Principles: Algorithms: A step-by-step set of operations to be performed. Analogy: Recipe, Instructions Abstraction: a technique for managing complexity. Analogy: Automobile, CS200 Computer View
23
Methods Lots of existing code that you can use rather than writing yourself. To use, "call the method". The method executes (runs) and may return a value.
24
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);
25
Defining Methods public class M { //method definition
static void mPrint() { System.out.println("my print"); } public static void main(String []args) { M.mPrint(); // method call. B is the correct answer
26
mPrint - which is Call, Definition?
mPrint call then mPrint definition static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); B is the correct answer
27
Argument vs. Parameter static void printCount(int count) {
public static void main(String []args) { int num = 10; printCount( 23); printCount( num+3); } static void printCount(int count) { 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).
28
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).
29
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);
30
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.
31
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.
32
Return to Converter double degreesCelsius; double degreesFahrenheit = 100.0; degreesCelsius = (degreesFahrenheit ) * 5.0 / 9.0;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.