CISC 1115
Introduction to Programming Using Java
Lecture 1
A Whirlwind Tour of Basic Java


What is Programming?

We want to develop a mechanism that allows for the specification and development of algorithms that can be performed by a machine

An Overview of the Programming Process

We will see there are three basic types of operations required to carry out any task: Examples Notice that the lines between the three basic types of operation are blurry (e.g., we can think of stirring the eggs as an action composed of a simpler action -- a single rotation of the fork repeated over and over again).

Algorithms

An algorithm is a step-by-step solution to a task problem

Specifying Computer Algorithms

We want to be able to specify algorithms that a machine will perform for us.

Unlike a person, a computer cannot fill in missing details, resolve ambiguities, or decide for itself what we probably meant.

We therefore need a formal mechanism for specifying exactly what we want the computer to do. That mechanism is a programming language.

What We Need from a Programming Language

If a programming language is to let us specify algorithms that a computer can carry out, what must it provide?

These are the essentials. Programming languages provide many other features that help us write programs that are clearer, easier to maintain, and able to grow as the problems we solve become larger.

There have been many such programming languages developed over the decades; the one we will be investigating is Java.

A Quick Introduction to Java

Basic Java Program Structure

Java programs have a basic common structure; every program (at least in this course) will have a similar layout and common text:

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

The Simplest Possible Java Program (App)

The simplest legal Java program does nothing … it consists of the above boilerpolate and nothing else; all that has been added is an actual class name:

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

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

A More Substantial Program

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

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

Hello world

A Program with a Calculation

We will introduce each of the above categories (actions, decision, repetition) individually, in programs of increasing functionality.

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

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:

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

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

  • 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
  • Adding a Decision (Conditional)

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

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

    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 hardcodedi; i.e., the actual values are written directly into each statement that uses them. This has several drawbacks, each of which points us toward the same solution.

    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 and 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 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 notion to be clearer.

    The Exam Averager Program Using Variables, Types, and Declarations

    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. The program should use variables for the values.

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

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

    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 typed at the keyboard (until a blank is encountered or 'return' key is pressed)
    		int midterm = scanner.nextInt();	// nextInt reads in an integer typed at the keyboard
    		…
    	}
    }
    		

    Prompting the User at the Keyboard

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

    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.

    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.

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

    Weiss 
    88
    92
    

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

    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

    The conditional we introduced back in Program P01.5 is actually introduce in all its glory in Lectur 3, and repetion — the for loop — is not discussed in detail until Lecture 9. We're presenting 'bay versions' of them now for several reasons:

    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.

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

    Weiss 
    88
    92
    Arnow
    75
    95
    Cox
    80
    100
    
    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
    

    The Grading Program: Letting the User Decide How Many Students to Process

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

    3
    Weiss 
    88
    92
    Arnow
    75
    95
    Cox
    80
    100
    
    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. Starting with the next lecture, we bring examining the three basic operations: imperative actions, conditionals, and repetition. We will also be examining other important language features that help us prganize oue programs as they get larger and more complex, as well as features to allow us to work with large amounts of data.

    Files Used in this Lecture

    Labs for this Lecture