Lec02 C
Lec02 C
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Class Logistics
Reminders
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Class Logistics
Environment
Programming Environment
IntelliJ
Platform
MacOS
Windows
Ubuntu Linux
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Class Logistics
Environment
Programming Environment
IntelliJ
Platform
MacOS
Windows
Ubuntu Linux
You don’t have to use this environment, but we may not be able to help
you if you don’t.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Why Java?
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Hello World
Hello World
1 package cse250.examples;
2
3 class MainExample
4 {
5 /**
6 * Main function
7 * @param args The arguments to main
8 */
9 public static void main(String[] args)
10 {
11 System.out.println("Hello World");
12 }
13 }
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Hello World
String args[]
There is a parameter args and its type is array of String.
public
The function can be called by anyone.
static
The function isn’t tied to an object (e.g.,
MainExample.main(...)).
void
The function doesn’t return anything.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Hello World
1 System.out.println("Hello World");
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Hello World
1 /**
2 * Main function
3 * @param args The arguments to main
4 */
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Hello World
1 package cse250.examples;
2
3 class MainExample
4 {
5 ...
6 }
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Exceptions
Exceptions
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Exceptions
Exceptions
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Exceptions
Catching Exceptions
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
1 class SHAZboT
2 {
3 public static void
4 doThings(String ILikeLlamas[])
5 {
6 String AString = "No";
7
8 // This is a for loop
9 for(q : ILikeLlamas) System.out.println(q);
10 }
11 }
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
1 class SHAZboT
2 {
3 public static void
4 doThings(String ILikeLlamas[])
5 {
6 String AString = "No";
7
8 // This is a for loop
9 for(q : ILikeLlamas) System.out.println(q);
10 }
11 }
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
Naming
SHAZboT
doThings
AString
ILikeLlamas
These are all valid variable names, but not helpful to someone
reading your code.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
Naming
SHAZboT
doThings
AString
ILikeLlamas
These are all valid variable names, but not helpful to someone
reading your code.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
Indentation
1 class SHAZboT
2 {
3 public static void doThings(String ILikeLlamas[])
4 {
5 String AString = "No";
6
7 // This is a for loop
8 for(q : ILikeLlamas) System.out.println(q);
9 }
10 }
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
Comments
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
Brackets
vs
1 for(q : ILikeLlamas)
2 {
3 System.out.println(q);
4 }
Java supports one-line for loops. This is one of the easiest ways to
introduce bugs.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
Brackets
vs
1 for(q : ILikeLlamas)
2 {
3 System.out.println(q);
4 }
Java supports one-line for loops. This is one of the easiest ways to
introduce bugs.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Java
Coding Style
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Ways to Succeed
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Ways to Succeed
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Ways to Succeed
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Ways to Succeed
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Ways to Succeed
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Typical questions:
Syntax (e.g., ”How do I break out of a for loop?”)
Ask on Piazza, Office Hours, Recitations!
Semantics (e.g., ”How do I insert an item into a linked list?”)
Ask, but help will usually not come in the form of code.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Typical questions:
Syntax (e.g., ”How do I break out of a for loop?”)
Ask on Piazza, Office Hours, Recitations!
Semantics (e.g., ”How do I insert an item into a linked list?”)
Ask, but help will usually not come in the form of code.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Basic Debugging
Demo
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Unit Testing
Look for this phrase
[part of code] should [do a thing]
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
Unit Testing
Look for this phrase
[part of code] should [do a thing]
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
JUnit
JUnit
1 package cse250.examples.debugging;
2
3 import org.junit.jupiter.api.Test;
4
5 public class BreakItDownTest {
6 ArrayList<FarmersMarket> data =
7 BreakItDown.readMarkets(/*...*/ );
8
9 @Test
10 void shouldCount75BakedGoods()
11 throws IOException
12 {
13 int count = BreakItDown.countTheBakedGoods(data);
14 assert (count == 75);
15 }
16 }
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
JUnit
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
JUnit
1 @Test
2 void shouldCount75BakedGoods()
3 throws IOException
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
JUnit
The test case should run the code you want to test, and then call
assert to confirm that the outputs are correct.
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .
CSE 250: Java Refresher
Strategies
JUnit
Demo
. . . . . . . . . . . . . . . . . . . .
© 2024 Oliver Kennedy, Eric Mikida, The University at Buffalo, SUNY
. . . . . . . . . . . . . . . . . . . .