Java Programming. Computer Science 112

Similar documents
Java Programming. Computer Science 112

Java Programming. Computer Science 112 Class 2

Java Programming. Computer Science 112

Java Programming. Computer Science 112

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

The Stack, Free Store, and Global Namespace

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

COMP 202 Java in one week

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Garbage Collection (1)

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Arrays. Storing Multiple Values

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

MITOCW watch?v=flgjisf3l78

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

MITOCW watch?v=4dj1oguwtem

The compiler is spewing error messages.

Class #1. introduction, functions, variables, conditionals

COMP-202 Unit 4: Programming with Iterations

Intro. Scheme Basics. scm> 5 5. scm>

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

MITOCW watch?v=0jljzrnhwoi

Lesson 2.4 Arraylist

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

Chapter 4 Lab. Loops and Files. Objectives. Introduction

MITOCW watch?v=9h6muyzjms0

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CS 302: Introduction to Programming

The following content is provided under a Creative Commons license. Your support

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Fortunately, you only need to know 10% of what's in the main page to get 90% of the benefit. This page will show you that 10%.

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Repetition CSC 121 Fall 2014 Howard Rosenthal

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

The following content is provided under a Creative Commons license. Your support

Read & Download (PDF Kindle) Programming: C ++ Programming : Programming Language For Beginners: LEARN IN A DAY! (C++, Javascript, PHP, Python, Sql,

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

BEGINNER PHP Table of Contents

6.001 Notes: Section 1.1

PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized

Repe$$on CSC 121 Spring 2017 Howard Rosenthal


Skill 1: Multiplying Polynomials

CSE : Python Programming

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

More on variables, arrays, debugging

Linked Lists. What is a Linked List?

CS 463 Project 1 Imperative/OOP Fractals

Assignment 2: Welcome to Java!

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

COP 3330 Final Exam Review

KOMAR UNIVERSITY OF SCIENCE AND TECHNOLOGY (KUST)

CS 106 Introduction to Computer Science I

Assignment 2.4: Loops

Lecture 2: Variables & Assignments

CS 302: Introduction to Programming in Java. Lecture 11 Yinggang Huang. CS302 Summer 2012

Appendix: Common Errors

Android Programming Family Fun Day using AppInventor

1 Getting started with Processing

Packages and Sharing Code. June 26, 2017

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

MITOCW watch?v=rvrkt-jxvko

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Repetition Structures

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

MEMORIZE the Java App pick: public class { public static void main(string[] a) {

MITOCW MIT6_172_F10_lec18_300k-mp4

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

Math Modeling in Java: An S-I Compartment Model

CS 360 Programming Languages Interpreters

CPSC W2 Midterm #2 Sample Solutions

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Week - 01 Lecture - 04 Downloading and installing Python

Last Class. More on loops break continue A bit on arrays

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

The following content is provided under a Creative Commons license. Your support

What is a variable? A named locajon in the computer s memory. A variable stores values

Post Experiment Interview Questions

Shorthand for values: variables

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

The following content is provided under a Creative Commons license. Your support

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Lecture Transcript While and Do While Statements in C++

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

What's the Slope of a Line?

Creating a Database Using Access 2003 for Windows 2000/Me/2003

Transcription:

Java Programming Computer Science 112

Review: Problem solving Class 4 is the Whole Point of Programming. Is there any particular one you'd like to go through? If you are desperately confused still, now is when you have my permission to be worried. Cumulative class, ya know. Could try 120 next semester - goes half as fast since C++ is a two semester class.

Recap: Variables A variable: Is a location in memory Has a type that defines what can be in that memory Has a cool identifier name Is really just a post-it note An Array is just a row of post-it notes The type of every note is the same You can look at any individual note you want

Moar Variables in a row Write a program that can average the grade of everyone in the class. int tpointsone; int tpointstwo; int tpointsthree; int tpointsfour; int tpointsfive; int tpointssix; // add them up and divide by 6 Oops, someone dropped the class. Recompile the program.

Don't wanna An array is a whole line of one type of variable in a row somearray = 3 5 16 4 3 12 11 9 8 0 1 2 3 4 5 6 7 8 The first row of numbers are the data on the string of post-it-sections. The second row is the "index" of each so you can refer to them directly

First glimpse of memory Flip through old assignments looking for the word "new" int tprimitive = 7; // No new here Scanner tkeyboard = new Scanner(System.in); // Eep! When you need a larger or more complicated post-it than what can hold a single int/float/boolean/char, you have to ask for it. Don't stress about the parenthesis yet, we still have a few weeks until that

Say the magic word int[] tgrades = new int[10]; int[] : An array of ints tgrades : Name for the variable = : Initializing the variable to something new : The magic word. I need some space. int : Will match the first word. Type of what is going in there. [10] : How many I want. Good tattoo

Secretly two steps here int[] tgrades = new int[10]; is the same as int[] tgrades; tgrades = new int[10]; Step one is Declaration. Second is Initialization. You can only declare once, but you can re-initialize all you want. This is important to remember because of scope. You can declare at the top of a program and initialize way later. Scope = A variable only exists between its closest set of curly braces.

Java loves you You don't need to know how many spaces you want when you write the program. int tderp = blah blah input; int[] therp = new int[tderp];// WAY more useful than C++ And all of the post-its are set to 0 by default. int tderp = 5; int[] therp = new int[tderp]; therp[4] = 5; therp[0] = 4; for( int i = 0; i < tderp; i++ ) // length - 1 is the last // 0 is the first System.out.println(tHerp[i]); // 4 0 0 0 5

See those indexes? (indices?) therp[4] = 5; therp[0] = 4; // length - 1 is the last // 0 is the first Square brackets are how you refer to individual variables If you refer to an index out of bounds, you crash. Remember how many times you should crash?

Normal variables inside There is nothing special about each individual variable in the array int[] tscores = new int[30]; tscores[4] = 5 + tscores[7]; Once you add the brackets, it is just a single int like before

for-loop is the champion For loops are for when you know how many times you want to do something You can ask an array how big it is Warning, idiom approaching Memorize it Tattoo it Love it int[] tscores = new int[30]; int tsum = 0; for( int i = 0; i < tscores.length; i++ ) tsum += tscores[i]; // += is "add this to me"

== is the goat You can't use == to compare two arrays. You can for int You can't for String An array is an Object, not a primitive type It needs a "new" There isn't a built-in way to check if "every member of both arrays is the same and the arrays are the same size too". What is this? An array for ants?

Sorting arrays! Everyone loves sorting algorithms! Nobody loves sorting algorithms. This is the only time the class will get desperately nerdy. Again, I lie to you. These are really important to know You will hate me in 30 minutes.

Selection Sort Start at spot 0. Find the smallest value in the array to the right and move it to that spot. Starting at the next number, do it again. 5 3 4 9 8 // Point at spot 0. 3 is the lowest, swap it. 3 5 4 9 8 3 4 5 9 8 3 4 5 9 8 3 4 5 8 9

Oh man I'm bored Textbook gets bored here too. Wikipedia has 20 different sorting methods. None of them matter because of:

ArrayList! Of course something so repetitive and pedestrian is built in. Programming is not about Stupid Human Tricks. It is about problem solving. I still have to at least mention some of the tricks though.

ArrayList vs array - Preview ArrayList has sorting and searching built in. But its syntax is goofy And using it for simple ints (Integer) is a pain Arrays take one line to make and you just need square brackets to use it. But it can't change size And it doesn't do any neat tricks ArrayList will be the better pick three weeks from now when everything is an Object I'm bringing this up just to explain why neither I nor the book feel like explaining the code for Bubble Sort.

Back to real life A normal array is a list of variables like we've shown. An array can also be two dimensional, so that it represents a spreadsheet or grid. int [][] tgrid = new int [5][10]; But there is controversy here! If you were to visualize this 5x10 table, is it wide or tall?

There is no spoon Wide or tall is whatever you define it to be. It's just bytes inside the computer. YOU must remember to be consistent. width, height? x, y? row, column? i, j? I'm a computer! Beep beep boop. To me, it is an array of arrays. [1 2 3 4 5] <- an array [[1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5]] <- An array of arrays. But whatever makes your brain get it is cool.

In-class program in 1D Ask user for size of array Create the array for-loop to fill all the values with a random number between 1 and 6 for-loop to display the array on a single line Remember println vs print Now add a while loop with a prompt to "Pick an index to add one to." Check out of bounds! Re-print the array after each change -1 to exit

In-class program in 2D Make the array 2D Fill up the array using a nested for loop - Imma put that on the board. Display all the values in a square Ask the user for a row and column to add one to This will get you half way through the homework, so it's important.

Let's flowchart the 2nd homework Remember, just draw out boxes using plain English. It's just brainstorming. Don't write any code until your chart handles all cases. Translate the chart one box at a time. And we can talk about the test. Tests are fun.

Now do 10 dimensions! jk Make the array Fill it with dots Pick a random spot for an 'X' Pick a different random spot for an 'o' Draw the two dimensional array Ask for input If entered 'x', print LOSE and break Change location of o (checking out of bounds) If on same spot as 'X', print WIN and break Back to Draw