51
CS 152 Professor: Leah Buechley TAs: Bethany Pena, Kayla Potter, Jared Tredway, Arlin Pedregon Time: MWF 10:00-10:50am Location: Zoom https://handandmachine.cs.unm.edu/classes/CS152_Fall2020/ Computer Programming Fundamentals

Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

CS 152 Professor: Leah Buechley

TAs: Bethany Pena, Kayla Potter, Jared Tredway, Arlin Pedregon Time: MWF 10:00-10:50am

Location: Zoom https://handandmachine.cs.unm.edu/classes/CS152_Fall2020/

Computer Programming Fundamentals

Page 2: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

JAVA

Page 3: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

open up your GettingStarted.java program from last class

open up Terminal/Command Prompt

Page 4: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

navigate to your GettingStarted folder

Page 5: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

THE COMMAND LINEJaeren's Handy Reference

MAC• cd = change directory• ls = list folders and

files within a directory• pwd = print the

directory that you’re currently in

PC• cd = change directory• dir = list folders and

files within a directory

Page 6: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

THE COMMAND LINE

• up arrow key will go through a history of your previous commands

• use it instead of typing stuff over and over

• the tab key will finish typing a command for you, if that is possible

• use it instead of typing long commands

Page 7: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

COMPILE & RUN “GettingStarted.java”COMPILE = javac

RUN = java

Page 8: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

COMPILING JAVA PROGRAMS

• Source code written in a .java file• Compiler converts source code into byte

code, a .class file• The Java Virtual Machine (JVM) runs this

byte code, translating it into instructions that your computer executes

Page 9: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

use dir or ls to see your .class file

Page 10: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

OUR FIBONACCI FUNCTION

Page 11: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …Xn = Xn-1 + Xn-2

THE FIBONACCI SEQUENCE

Page 12: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

OUR FUNCTION SO FAR (HAS ERRORS)

public static long fibonacci (long input) {long result = 0;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 13: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

OUR FUNCTION SO FAR (HAS ERRORS)public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();factorial(number);

}

public static long fibonacci (long input) {long result = 0;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 14: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

OUR FUNCTION SO FAR (HAS ERRORS)public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();System.out.println(fibonacci(number));

}

public static long fibonacci (long input) {long result = 0;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 15: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

save, compile, and run

Page 16: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

OUR FUNCTION SO FAR (HAS ERRORS)public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();System.out.println(fibonacci(number));

}

public static long fibonacci (long input) {long result = 0;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 17: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

OUR FUNCTION SO FAR (HAS ERRORS)public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();System.out.println(fibonacci(number));

}

public static long fibonacci (long input) {long result = input;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 18: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

save, compile, and run

Page 19: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

ADD A FOR LOOP TO PRINT THE SEQUENCEpublic static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();for (int i=0; i<number; i++) {

}}

public static long fibonacci (long input) {long result = input;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 20: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

ADD A FOR LOOP TO PRINT THE SEQUENCEpublic static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();for (int i=0; i<number; i++) {

System.out.print(fibonacci(i) + ", ");}

}

public static long fibonacci (long input) {long result = input;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 21: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

ADD A FOR LOOP TO PRINT THE SEQUENCEpublic static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Type in a number:");int number = sc.nextInt();for (int i=0; i<number; i++) {

System.out.print(fibonacci(i) + ", ");}

System.out.println(fibonacci(number));}

public static long fibonacci (long input) {long result = input;

if (input == 1 | input == 0)return result;

else {result = fibonacci(input - 1) + fibonacci(input - 2);return result;

}

}

Page 22: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

FIBONACCI SPIRAL

Page 23: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

More info: https://www.mathsisfun.com/numbers/nature-golden-ratio-fibonacci.html

Page 24: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

STRINGS

Page 25: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

create a new program “LetterCount.java”save it in a folder called LetterCount

Page 26: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTclass LetterCount {

}

Page 27: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

programwill prompt you to enter a wordwill prompt you to enter a letter

will count the number of times the letter appears in the word and print out the count

Page 28: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

will use the Scanner class again

Page 29: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

}

Page 30: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {

}

}

Page 31: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

Java reference: https://docs.oracle.com/en/java/javase/15/ other references: https://www.w3schools.com/java/default.asp

+ google is your friend!

HOW TO GET HELP

Page 32: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

prompt for a word

Page 33: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

how do we read a word from the Scanner?

Page 34: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

good reference for Scanner: https://www.w3schools.com/java/java_user_input.asp

Page 35: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

good reference for Strings:https://www.w3schools.com/java/java_ref_string.asp

Page 36: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {Scanner sc = new Scanner(System.in);

//ask for a wordSystem.out.println("Type in a word:");String word = sc.next();

}

}

Page 37: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

save, compile, and runin general, save and compile often

Page 38: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

prompt for a letter

Page 39: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

how do we read a letter from the Scanner?

Page 40: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

good reference for Scanner: https://www.w3schools.com/java/java_user_input.asp

Page 41: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

good reference for Strings:https://www.w3schools.com/java/java_ref_string.asp

Page 42: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {Scanner sc = new Scanner(system.in);

//ask for a wordSystem.out.println("Type in a word:");String word = sc.next();

//ask for a letterSystem.out.println("Type in a letter:");char letter = sc.next().charAt(0);

}

}

Page 43: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

save, compile, and run

Page 44: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

count number of times letter appears in word

Page 45: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {Scanner sc = new Scanner(System.in);

//ask for a wordSystem.out.println("Type in a word:");String word = sc.next();

//ask for a letterSystem.out.println("Type in a letter:");char letter = sc.next().charAt(0);

//search through the entire word int count = 0;

for (int i = 0; i<word.length(); i++) {

}}

}

Page 46: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {Scanner sc = new Scanner(System.in);

//ask for a wordSystem.out.println("Type in a word:");String word = sc.next();

//ask for a letterSystem.out.println("Type in a letter:");char letter = sc.next().charAt(0);

//search through the entire word int count = 0;

for (int i = 0; i<word.length(); i++) {if (word.charAt(i) == letter)

count++;}

}

}

Page 47: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

print out the count

Page 48: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {Scanner sc = new Scanner(System.in);

//ask for a wordSystem.out.println("Type in a word:");String word = sc.next();

//ask for a letterSystem.out.println("Type in a letter:");char letter = sc.next().charAt(0);

//search through the entire word int count = 0;

for (int i = 0; i<word.length(); i++) {if (word.charAt(i) == letter)

count++;}

//print out the countSystem.out.println(word +" has " +count +" " +letter +"'s.");

}

}

Page 49: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {Scanner sc = new Scanner(System.in);

//ask for a wordSystem.out.println("Type in a word:");String word = sc.next();

//ask for a letterSystem.out.println("Type in a letter:");char letter = sc.next().charAt(0);

//search through the entire word int count = 0;

for (int i = 0; i<word.length(); i++) {if (word.charAt(i) == letter)

count++;}

//print out the countif (count == 0)

System.out.println(word +" has no " +letter +"'s.");else if (count == 1)

System.out.println(word +" has 1 " +letter +".");else

System.out.println(word +" has " +count +" " +letter +"'s.");}

}

Page 50: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

questions?

Page 51: Computer Programming Fundamentals · 2020. 10. 21. · THE COMMAND LINE Jaeren's Handy Reference MAC • cd = change directory • ls = list folders and files within a directory

CS 152 Professor: Leah Buechley

TAs: Bethany Pena, Kayla Potter, Jared Tredway, Arlin Pedregon Time: MWF 10:00-10:50am

Location: Zoom https://handandmachine.cs.unm.edu/classes/CS152_Fall2020/

Computer Programming Fundamentals