computer programming fundamentals · 2020. 10. 21. · the command line jaeren's handy...

Post on 05-Sep-2021

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

JAVA

open up your GettingStarted.java program from last class

open up Terminal/Command Prompt

navigate to your GettingStarted folder

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

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

COMPILE & RUN “GettingStarted.java”COMPILE = javac

RUN = java

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

use dir or ls to see your .class file

OUR FIBONACCI FUNCTION

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

THE FIBONACCI SEQUENCE

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;

}

}

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;

}

}

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;

}

}

save, compile, and run

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;

}

}

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;

}

}

save, compile, and run

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;

}

}

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;

}

}

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;

}

}

FIBONACCI SPIRAL

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

STRINGS

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

LETTER COUNTclass LetterCount {

}

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

will use the Scanner class again

LETTER COUNTimport java.util.Scanner;

class LetterCount {

}

LETTER COUNTimport java.util.Scanner;

class LetterCount {

public static void main(String[] args) {

}

}

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

prompt for a word

how do we read a word from the Scanner?

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

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

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();

}

}

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

prompt for a letter

how do we read a letter from the Scanner?

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

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

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);

}

}

save, compile, and run

count number of times letter appears in word

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++) {

}}

}

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 count

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.");

}

}

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.");}

}

questions?

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

top related