comp 401 introduction
Download
Skip this Video
Download Presentation
Comp 401 Introduction

Loading in 2 Seconds...

play fullscreen
1 / 39

Comp 401 Introduction - PowerPoint PPT Presentation


  • 124 Views
  • Uploaded on

Comp 401 Introduction. Instructor: Prasun Dewan. Comp 401 vs. 110. Majors vs. Non Majors? Majors usually start with 401 But many 110 students become majors. Object-oriented vs. Conventional? Both 401 and (current) 110 focus on objects. Java vs. Non-Java? 110 and 401 are both in Java

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Comp 401 Introduction' - virote


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
comp 401 introduction

Comp 401Introduction

Instructor: PrasunDewan

comp 401 vs 110
Comp 401 vs. 110
  • Majors vs. Non Majors?
      • Majors usually start with 401
      • But many 110 students become majors.
  • Object-oriented vs. Conventional?
    • Both 401 and (current) 110 focus on objects.
  • Java vs. Non-Java?
    • 110 and 401 are both in Java
    • Language is not the issue

401

110

CS Majors

Psychology, Biology, …

Functional, Imperative, …

Object-Oriented

Java

C++, Python, …

comp 401 vs 1101
Comp 401 vs. 110
  • “Intermediate” vs. “introductory” programming
    • Introductory may be object-oriented
    • Introductory may be conventional
    • “Introductory” material must have few language and course dependencies
    • Assume background in conventional programming and will teach Java syntax for it.
    • Repetition for those who know object-oriented programming.

401

110

Intermediate

Introductory

introductory conventional programming
Introductory Conventional Programming
  • Types, variables, assignment , constants, expression
  • Conditionals, loops.
  • Input and output
  • Arrays
  • Procedures/Functions/Subroutines/Methods
  • Comments
  • Program vs. algorithm
types variables assignment constant expressions
Types, Variables, Assignment, Constant, Expressions
  • Type
  • Variable
  • Constant
  • Named constant
  • Assignment
  • Expression
  • Type rules determine legal and illegal assignments

double height = 1.77;

booleanoverWeight = false;

intHIGH_BMI = 27;

Stringname =“joe”;

char firstChar= name.charAt(0);

intbmi= (int) weight/(height * height);

intweight = “seventy”;

conditionals and output
Conditionals and Output

if (score < PASS_CUTOFF) {

System.out.println("**************");

System.out.println("FAILED");

System.out.println("**************");

}

else {

System.out.println("**************");

System.out.println("PASSED");

System.out.println("Congratulations!");

System.out.println("**************");

}

while loops and input
While Loops and Input
      • intproduct = 1;
      • intnextNum = Console.readInt();
      • while (nextNum >= 0) {
      • product = product* nextNum;
      • nextNum = Console.readInt();
      • }
  • print (product);
for loops arrays and comments
For Loops, Arrays and Comments

System.out.println("Number of Strings:");

intnumElements = Console.readInt(); // reads the next line as integer

System.out.println("Please enter " + numElements + " strings");

String[] strings = new String[numElements]; // dynamic array

for (intelementNum = 0; elementNum < numElements; elementNum++)

strings[elementNum] = Console.readString();

/* This loop uses the array input

** in the previous loop*/

for ( intelementNum = 0; elementNum < strings.length; elementNum++)

System.out.println(strings[elementNum]);

String s = strings[0]; // unsafe

for (inti=0; i<s.length(); i++)

System.out.println(s.charAt(i));

Difference in syntax: arrays built into language, strings are library

accessing substrings
Accessing Substrings

s.substring(beginIndex, endIndex)

“hello world”.substring(4,7)

 “o w”

“hello world”.substring(4,4)

 “”

“hello world”.substring(7,4)

StringIndexBounds exception

methods procedures functions
Methods/Procedures/Functions

static intf (int n) {

intproduct = 1;

while (n > 0) {

product *= n;

n -= 1;

}

return product;

}

  • Called function
  • Takes int argument, n,
  • Returns int

1*2*3*…*n

  • Calling procedure.
  • Takes String array argument
  • Returns nothing – void

public static void main (String[] args) {

while (true) { // loop condition never false

int n = Console.readInt();

if (n < 0)

break;

System.out.println("factorial = " + f(n));

}

}

Static implies non-object oriented programming.

call chains
Call Chains

R

Main method starts the computation, and can call other methods.

Q

Can put complete program in main method

Like having one big paragraph in an essay

P

Method decomposition important modularization technique even in conventional programming

main

main method details
Main Method Details

Main method has predefined header.

R

public static void main (String[] args) {

….

}

Q

All methods must be in some “class” (file, which can be in a “package” (directory)

P

package warmup;

publicclassAnArgPrinter {

publicstaticvoid main (String[] args) {

System.out.println (args[0]);

}

}

main

The Java interpreter calls main and provides its user-specified argument. Public means interpreter can access main.

running main class
Running Main Class

package warmup;

publicclassAnArgPrinter {

publicstaticvoid main (String[] args) {

System.out.println (args[0]);

}

}

Array of user-supplied strings

Interpreter

Package

Class

Output

User-Supplied Argument

array subscript error
Array Subscript Error

package warmup;

publicclassAnArgPrinter {

publicstaticvoid main (String[] args) {

System.out.println (args[0]);

}

}

Subscript Error

User-Supplies No Argument

safe arg printer edit in class
Safe ArgPrinter (edit in class)

package warmup;

publicclassAnArgPrinter {

publicstaticvoid main (String[] args) {

System.out.println (args[0]);

}

}

safe arg printer1
Safe ArgPrinter

package warmup;

publicclassAnArgPrinter {

publicstaticvoid main (String[] args) {

if (args.length == 1)

System.out.println (args[0]);

else

System.out.println("Illegal no of arguments:" + args.length + ". Terminating program");

}

}

String concatenation

scanning problem1
Scanning Problem
  • Scanning image for text.
  • Scanning frequencies for radio stations.
  • Finding words in a sentence
  • Finding identifiers, operators, in a program
scanning

token

token

J

o

h

n

F

.

K

e

n

n

e

d

y

token

token

token

Scanning

Input

stream

Token

Stream

algorithm

marker

0

Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

Output: J

algorithm1
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

1

Output: J

algorithm2
Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

2

Output: J

algorithm3
Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

5

Output: JF

algorithm4
Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

6

Output: JF

algorithm5
Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

8

Output: JFK

algorithm6
Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

9

Output: JFK

algorithm7
Algorithm

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

14

Output: JFK

solution edit in class
Solution (edit in class)

packagewarmup;

publicclassAnUpperCasePrinter {

publicstaticvoid main(String[] args){

}

}

solution edit in class1
Solution (edit in class)

packagewarmup;

publicclassAnUpperCasePrinter {

publicstaticvoid main(String[] args){

if (args.length != 1)

System.esit(0);

for (int index = 0; index < args.length; index++)

if (Character.isUpperCase(args[0].charAt(index))

System.out.print(args[0].charAt(index));

}

}

solution
Solution

packagewarmup;

publicclassAnUpperCasePrinter {

publicstatic void main(String[] args){

if (args.length != 1) {

System.out.println("Illegal number of arguments:" + args.length + ". Terminating program.");

System.exit(-1);

}

System.out.println("Upper Case Letters:");

int index = 0;

while (index < args[0].length()) {

if (Character.isUpperCase(args[0].charAt(index)))

System.out.print(args[0].charAt(index));

index++;

}

System.out.println();

}

Print on new vs previous line

beyond introductory programming
Beyond Introductory Programming
  • Comp 110: Creating small simple programs
    • Main and a few classes
  • Comp 401: Creating large programs
    • reusability and understandability
    • individual pieces simple
    • project helps
  • Comp 410: Programming complex code
    • complex popular data structures
    • non-trivial efficiency analysis
ad