CISC 1115
Introduction to Programming Using Java
Lecture 1
Introduction and Overview


Reading from the Text

You should be able to begin reading the text from the beginning; the authors have a clear presentation. Here are specific references to material covered in this lecture. Realize this is an overview of what we will be doing, so the section mentioned will be from various chapters and may refer to material we haven't covered yet (or at least not in detail).

Chapter 1

Chapter 2

Chapter 3

Chapter 5

Some Motivation

How Would You Walk From Brooklyn College To Newkirk Plaza?

Here's an answer

How Do You Make A Peanut Butter and Jelly Sandwich?

Here's an answer

How Do You Multiply Two 3-digit Numbers?

Here's an answer Some terms and concepts that are related to the above discussions:

What is Programming?

An Overview of the Programming Process

We will see there are three basic types of operations required to carry out any task: A decent (though not perfect) analogy of the programming process is creating and then carrying out a recipe

A Basic Overview of Recipes and Programs

Recipes Structure

Imagine buying a recipe box with blank recipe cards:

Notes

(Java) Program Structure

Java programs also have a basic common structure; every program will have a similar layout and common text:

Code
1. public class <ClassName> { 2. public static void main(String [] args) { 3. 4. } 5. 6. }
Notes

A Trivial Recipe: Cereal & Milk

Notes

A Trivial Zero'th Program

We provide an equally simple first program, adding no code; all that needs to be added is the name:

Program P01.1 The Empty Program

Write a program that does nothing except contain the required boilderplate; i.e., the empty program

Code
public class EmptyApp {
	public static void main(String [] args) {
	}
}
Notes

A Somewhat Better First Program

Program P01.2 HelloWorld

Write a program that displays the text Hello world on the screen.

Notes
Code

public class HelloWorld { public static void main(String [] args) { System.out.println("Hello world"); } }

Output
Hello world

Notes

A More Substantive (but still simple) Recipe: Fried Eggs

Fried Eggs

Here is a recipe with a little more substance:

Ingredients

Instructions
Looking a bit more carefully at this we see the three basic categories of operations introduced above:

P01.3 — A More Substantive Program

With regard to programs, we are going to proceed more slowly: we will introduce each of the above categories individually, in programs of increasing functionality.

P01.3 ExamAverager

Write a program that calculates the average of a midterm grade of 78 and a final exam grade of 86.

public class ExamAverager {
	public static void main(String [] args) {
		System.out.println((78+86)/2);
	}
}
Output
82

Notes

P01.4 — Making the Output More Readable

We'd like to add some descriptive text to out output to let the user know what they're looking at:

P01.4 ExamAverager

Write a program that calculates the average of a midterm grade of 88 and a final exam grade of 92 for the student Gerald Weiss and prints out the result together with some descriptive text.

public class ExamAverager {
	public static void main(String [] args) {
		System.out.println("Gerald Weiss received an 88 and a 92 for an average of " + (88 + 92) / 2); 
	}
}

Output
Gerald Weiss received an 88 and a 92 for an average of 90

Notes
  • All we've done here is add some descriptive text to the println
  • When at least one of the operands of + is a String, the operator is called the concatenation operator.
  • The action of the concatentation operator is to textually join the first and second operands
    • For example, "Hello " + "world" results in "Hello world" (notice the trailing blank at the end of the string "Hello ".
    • If one of the operands is a string and the other is something else (right now, the only other thing we have is a number), the number is treated as text, and concatenated to the string. Some examples:
      • "The number is " + 15 produces "The number is 15"
      • "The average is " + (2+4)/2 produces "The average is 3"
      • "The sum is " + 2 + 4 produces "The sum is 24"?!
        • This last example shows us we need to be careful about using parentheses when concatenating
  • P01.5 — Adding a Decision (Conditional)

    Let's add an example of the second category of operations: decision-making.

    P01.5 ExamAverager

    Write a program that calculates the average of a midterm grade of 88 and a final exam grade of 92 for the student Gerald Weiss, and prints whether the student has passed or failed the course based on a passing grade of 60.

    public class ExamAverager {
         public static void main(String [] args) {
              System.out.println("Gerald Weiss received an 88 and a 92 for an average of " + (88 + 92) / 2);
              if ((88 + 92) / 2 >= 60)
                   System.out.println("Gerald Weiss passes");
              else
                   System.out.println("Gerald Weiss fails");
         }
    }
    

    Notes

    Output
    Gerald Weiss received an 88 and a 92 for an average of 90
    Gerald Weiss passes
    

    Introducing Variables and Types

    In the above code, all the values are hardcoded they appear constantly wherever they are used. There are several issues with this approach, all of which lead to the introduction of the same concept.

    Avoiding Redundant or Repetitious Values

    Allowing Different Names and Exam Values

    The above program only provides information for a student named Gerald Weiss who received exam grades of 88 and 92. We might simply want to be able to quickly change the name of grades and run the program again.

    Making the Code More Readable

    Looking at the original (88 + 92) / 2 program, it takes a bit of imagination to first realize that an average is being taken and then even more so that it is the average of two exam grades. We would like that to be clearer.

    P01.6 — The Exam Averager Program Using Variables, Types, and Declarations

    P01.6 ExamAverager

    Write a program that calculates the average of a midterm grade of 88 and a final exam grade of 92 for the student Gerald Weiss, and prints whether the student has passed or failed the course based on a passing grade of 60.

    public class ExamAverager {
    	public static void main(String [] args) {
    		String name = "Gerald Weiss";
    		int midterm = 88;
    		int finalExam = 92;
    		int average = (midterm + finalExam) / 2;
    		System.out.println(name + " received an " + midterm + " and a " + finalExam + " for an average of " + average);
    		if (average >= 60)
    			System.out.println(name + " passes");
    		else
    			System.out.println(name + " fails");
    	}
    }
    

    Output
    Gerald Weiss received an 88 and a 92 for an average of 90
    Gerald Weiss passes
    

    Notes
    Introducing variables addresses and resolves the issues presented at the beginning of this section:

    Making Use of the 'Variable' In Variable

    Reading Values (Input) From the Keyboard

    import java.util.Scanner;
    
    public class <ClassName> {
    	public static void main(String [] args) {
    		Scanner scanner = new Scanner(System.in);
    		…
    		String name = scanner.next();		// next reads in text (until the next blank or 'return' key) typed at the keyboard
    		int midterm = scanner.nextInt();		// nextInt reads in an integer typed at the keyboard
    		…
    	}
    }
    		

    Notes

    User Prompts

    System.out.print("Name? ");
    String name = scanner.next();
    

    Notes
    Notice that no new Java was introduced to solve this problem, merely understanding the issue and applying a clever / common-sense approach. We'll call such an approach a technique; we will encounter many such techniques over the course of the semester.

    P01.7 — The Exam Grader With Input From the Keyboard

    We can now present a program that prompts the user and accepts data typed in from the keyboard.

    P01.7 ExamAverager

    Modify P01.6 so the data is read from the keyboard.

    import java.util.Scanner;
    
    public class ExamAverager {
    	public static void main(String [] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		System.out.print("Name? ");
    		String name = scanner.next();
    		System.out.print("Midterm? ");
    		int midterm = scanner.nextInt();
    		System.out.print("Final? ");
    		int finalExam = scanner.nextInt();
    
    		int average = (midterm + finalExam) / 2;
    		System.out.println(name + " received an " + midterm + " and a " + finalExam + " for an average of " + average);
    		if (average >= 60)
    			System.out.println(name + " passes");
    		else
    			System.out.println(name + " fails");
    	}
    }
    

    Input
    Weiss 
    88
    92
    

    Output
    Name? Midterm? Final? Weiss received an 88 and a 92 for an average of 90
    Weiss passes
    

    Notes

    The Interactive Session

    The above Input and Output showed exactly what is typed in at the keyboard and printed out by the System.out.println's (and System.out.print's) of the program. However, since what is typed at the keyboard is echoed on the screen, what you see displayed looks somewhat different than the above output:

    Here is a sample execution of the program.
    User input is in bold. Your program should replicate the prompts and output:

    Name? Weiss
    Midterm? 88
    Final? 92
    Weiss received an 88 and a 92 for an average of 90
    Weiss passes

    Adding Repetition

    Repeating Sections of Code — the for Loop

    P01.8 — The Grading Program for Three Students

    We can now present a program that repeats our grading logic for several students. A for loop will provide the repetition and we will use a Scanner to read the sets of data from the keyboard.

    P01.8 ExamAverager

    Modify P01.7 so it processes three students before terminating.

    import java.util.Scanner;
    
    public class ExamAverager {
    	public static void main(String [] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		for (int i = 1; i <= 3; i = i + 1) {
    			System.out.print("Name? ");
    			String name = scanner.next();
    			System.out.print("Midterm? ");
    			int midterm = scanner.nextInt();
    			System.out.print("Final? ");
    			int finalExam = scanner.nextInt();
    
    			int average = (midterm + finalExam) / 2;
    			System.out.println(name + " received an " + midterm + " and a " + finalExam + " for an average of " + average);
    			if (average >= 60)
    				System.out.println(name + " passes");
    			else
    				System.out.println(name + " fails");
    		}
    	}
    }
    

    Notes

    Input
    Weiss 
    88
    92
    Arnow
    75
    95
    Cox
    80
    100
    

    Output
    Name? 
    Midterm? 
    Final? 
    Weiss received an 88 and a 92 for an average of 90
    Weiss passes
    Name? 
    Midterm? 
    Final? 
    Arnow received an 75 and a 95 for an average of 85
    Arnow passes
    Name? 
    Midterm? 
    Final? 
    Cox received an 80 and a 100 for an average of 90
    Cox passes
    

    P01.9 — The Grading Program: Letting the User Decide How Many Students to Process

    P01.9 ExamAverager

    Modify P01.8 so that the user is prompted for the number of students to process.

    public class ExamAverager {
    	public static void main(String [] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		System.out.print("How many students do you wish to process? ");
    		int howMany = scanner.nextInt();
    
    		for (int i = 1; i <= howMany; i = i + 1) {
    			System.out.print("Name? ");
    			String name = scanner.next();
    			System.out.print("Midterm? ");
    			int midterm = scanner.nextInt();
    			System.out.print("Final? ");
    			int finalExam = scanner.nextInt();
    
    			int average = (midterm + finalExam) / 2;
    			System.out.println(name + " received an " + midterm + " and a " + finalExam + " for an average of " + average);
    			if (average >= 60)
    				System.out.println(name + " passes");
    			else
    				System.out.println(name + " fails");
    		}
    	}
    }
    

    Input
    3
    Weiss 
    88
    92
    Arnow
    75
    95
    Cox
    80
    100
    

    Output
    How many students do you wish to process? 
    Name? 
    Midterm? 
    Final? 
    Weiss received an 88 and a 92 for an average of 90
    Weiss passes
    Name? 
    Midterm? 
    Final? 
    Arnow received an 75 and a 95 for an average of 85
    Arnow passes
    Name? 
    Midterm? 
    Final? 
    Cox received an 80 and a 100 for an average of 90
    Cox passes
    

    Notes

    Summary

    The above examples illustrated all three fundamental coding tools: actions, decisions, and repetition. We will now return to each of them in much greater detail.

    The Next Level: Organization and Responsibility

    Let's revisit the recipe world for a moment and look at a more complex egg recipe:

    A More Complex Recipe: Huevos Rancheros

    Ingredients Instructions Salsa

    a salsa recipe would go here

    Methods and Classes

    Similarly with software development; which is typically much more complicated than most recipes. To control such complexity we will organize our code into various levels.

    Methods

    Methods are logical units of code that represent subtasks. We will cover methods shortly; deciding what should be turned into a method, and then designing the method proper takes practice.

    Classes

    Classes are just collections of resources — methods and data (variables). Deciding what should be a class and how to design it — as with methods — requires practice (even more so); and this is deferred to the next course in the sequence.

    But, remember, at the end of the day, all that is needed to accomplish any programmable task are actions, decisions, and repetition; methods and classes are 'merely' organizational elements.

    One Last Thing to Think About

    Turtles All the Way Down

    … and All the Way Up

    Where Do We Go From Here?

    Files Used in this Lecture

    Labs for this Lecture