PROGRAM STRUCTURE CSC 111.

Slides:



Advertisements
Similar presentations
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Your First Java Program: HelloWorld.java
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
How to Create a Java program CS115 Fall George Koutsogiannakis.
CMT Programming Software Applications
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CSC 111 Java Programming I. Java Programming: From Problem Analysis to Program Design, Second Edition  Instructor – Salwa Hamad Al-Jasser  Office.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Chapter 1: An Overview of Computers and Programming Languages
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
An Introduction to the Java Language App Design Flat Rock Community Schools Introductory Java Programming.
Foundations of Programming: Java
ARRAYS.
Computer Programming Your First Java Program: HelloWorld.java.
Introduction to Java Applications
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
Chapter 3 GC 101 Java Fundamentals.
PowerPoint Presentation Authors of Exposure Java
Chapter 2 - Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 2 - Introduction to C Programming
Intro to Java.
SELECTION STATEMENTS (1)
INPUT STATEMENTS GC 201.
OPERATORS (1) CSC 111.
IDENTIFIERS CSC 111.
OPERATORS (2) CSC 111.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
PROBLEM SOLVING CSC 111.
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Fundamentals of Programming
MSIS 655 Advanced Business Applications Programming
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Programs written in C and C++ can run on many different computers
Chapter 2 - Introduction to C Programming
In this class, we will cover:
CSC 1051 – Data Structures and Algorithms I
Unit 3: Variables in Java
Running a Java Program using Blue Jay.
Computer Programming-1 CSC 111
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

PROGRAM STRUCTURE CSC 111

Outline 1. Programming Approaches 2. Object-Oriented Programming 3. Program Structure 4. Output Statements

1. PROGRAMMING APPROACHES Two programming approaches are known: The Structured Programming Also known as modular programming. The problem is divided into smaller sub-problems (modules). Each sub-problem is then analyzed and solved. The solutions of all sub-problems are then combined to solve the overall problem. Examples of such programming model languages include Pascal, Fortran and C. The Object-Oriented Programming Identify the components of the problem. These are called objects. For each object, identify the relevant data & operations (methods) to be performed on that data. Define the relationship between each object and the other. Examples of programming languages that follow such model are C++ and Java. OBJECTS DATA METHODS

2. OBJECT-ORIENTED PROGRAMMING EXAMPLE 1 A program is needed by a local store that rents videos to customers. Identify the objects of such program. Also, specify the data and methods for each identified object. Objects: Video Customer Video Data Movie name Starring actors Production company Production date Number of copies in the store Methods Reduce number of copies Add number of copies Customer Data Customer name Customer ID VIP Methods Add a customer Remove a customer

2. OBJECT-ORIENTED PROGRAMMING EXAMPLE 2 A program is needed by a university Registrar. Identify the objects of such program. Also, specify a few data and methods for each identified object. Objects: Student Course Student Data Student name Student ID GPA Methods Add a student Remove a student Print a student’s schedule Update a student’s GPA Course Data Course code Course name Pre-requisites Co-requisites Methods Add a course Remove a course Define schedule

3. PROGRAM STRUCTURE PROGRAM LAYOUT // import necessary libraries public class ProgramLayout { public static void main (String[] args) // Declaration section: to declare needed variables // Input section: to enter values of used variables // Processing section: processing statements // Output section: display program output } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11

4. OUTPUT STATEMENTS The following is a simple Java program: // This is my first Java program public class Welcome { public static void main (String[] args) System.out.println (“Welcome to Java”); } 1 2 3 4 5 6 7 8 When you compile and execute this program, you will get the following output: Welcome to Java 1

4. OUTPUT STATEMENTS PROGRAM 1 - EXPLAINED Line Symbol/Word // This is my first Java program public class Welcome { public static void main (String[] args) System.out.println (“Welcome to Java”); } 1 2 3 4 5 6 7 8 Line Symbol/Word Explanation 1 // Precedes a comment. A comment is discarded by the compiler. The comment ends with the end of the line. Comments are written to document (clarify) the program. 2 public Reserved word to Java. To be explained later. class Reserved word to Java. Any Java program must start with a class. A class includes one or more methods. Welcome Class name. When a Java program is saved, it is usually given the class name. The system automatically gives the extension “.java”. So, in this example, the name of the file on the disk is “welcome.java”.

PROGRAM 1 – EXPLAINED (cnt’d) 4. OUTPUT STATEMENTS PROGRAM 1 – EXPLAINED (cnt’d) // This is my first Java program public class Welcome { public static void main (String[] args) System.out.println (“Welcome to Java”); } 1 2 3 4 5 6 7 8 Line Symbol/Word Explanation 3 { Opening brace to the class Welcome. Any opening brace must be closed in the program. This brace is closed in line 8. 4 public static void String[] args Reserved words to Java. To be explained later. main This is a non-optional name to the “main” method. A Java class should have at most one “main” method. Program execution thread always begins with the “main” method. 5 Opening brace to the “main” method. This brace closes at line 7.

PROGRAM 1 – EXPLAINED (cnt’d) 4. OUTPUT STATEMENTS PROGRAM 1 – EXPLAINED (cnt’d) // This is my first Java program public class Welcome { public static void main (String[] args) System.out.println (“Welcome to Java”); } 1 2 3 4 5 6 7 8 Line Symbol/Word Explanation 6 System.out.println (“Welcome to Java”) This is an output statement. The words enclosed between double quotes are called a string. “Welcome to Java” is a string. The string is displayed on the screen as it appears exactly in the program. The double quotes are not printed. (Refer to slide 6 to see the output). ; The semicolon ends the output statement. 7 } The closing brace to the “main” method. 8 The closing brace to the “Welcome” class.

PROGRAM 1 – EXPLAINED (cnt’d) 4. OUTPUT STATEMENTS PROGRAM 1 – EXPLAINED (cnt’d) // This is my first Java program public class Welcome { public static void main (String[] args) System.out.println (“Welcome to Java”); } 1 2 3 4 5 6 7 8 In Java programs, braces are nested. In other words, the brace that opens first, closes last. (Refer to the figure)

4. OUTPUT STATEMENTS The program output is as follows: PROGRAM 2 /* Author name: XYZ This is another simple Java program */ public class SimpleProgram { public static void main (String[] args) System.out.println (“The sum of 2 and 3 is ” + 5); System.out.println (“7 + 8 = “ + (7 + 8) ); } 1 2 3 4 5 6 7 8 9 10 11 The program output is as follows: The sum of 2 and 3 is 5 7 + 8 = 15 1 2

4. OUTPUT STATEMENTS PROGRAM 2 – EXPLAINED Line Symbol/Word /* Author name: XYZ This is another simple Java program */ public class SimpleProgram { public static void main (String[] args) System.out.println (“The sum of 2 and 3 is ” + 5); System.out.println (“7 + 8 = “ + (7 + 8) ); } 1 2 3 4 5 6 7 8 9 10 11 Line Symbol/Word Explanation 1 /* Starts a multi-line comment. 3 */ Ends a multi-line comment. 4 SimpleProgram Class name. 8 “The sum of 2 and 3 is” This string is printed as it is written in the program like the previous example. + Strings concatenation operator. It joins the string with 5. The output is shown in the previous slide. 5 The system automatically converts the number 5 into a string to concatenate it with the first string.

PROGRAM 2 – EXPLAINED (cnt’d) 4. OUTPUT STATEMENTS PROGRAM 2 – EXPLAINED (cnt’d) /* Author name: XYZ This is another simple Java program */ public class SimpleProgram { public static void main (String[] args) System.out.println (“The sum of 2 and 3 is ” + 5); System.out.println (“7 + 8 = “ + (7 + 8) ); } 1 2 3 4 5 6 7 8 9 10 11 Line Symbol/Word Explanation 9 “7 + 8 = “ This string will print as is. (7 + 8) The parenthesis instruct the compiler to add the two numbers resulting in 15. + Concatenation operator. Joins the first string with the number 15. The number 15 is converted to a string before being joined to the first string. The effect of the + operator is determined according to the surrounding operands. If it is between strings, it “concatenates”; if it is between numbers, it “adds”.

Self-Check Exercises (1) A program is needed for a local store that rents cars. Identify the objects in this program. Also, specify a few data and methods of each identified object. A program is needed for a hospital. Identify objects in this program. Also, specify a few data and methods of each identified object. What is the output of the following program: // This is an exercise public class SimpleProgram { public static void main (String[] args) System.out.println (“5 multiplied by 8 is ” + 40); System.out.println (“5 * 8 = “ + (5 * 8) ); } 1 2 3 4 5 6 7 8 9 W2.1 Program Structure

Self-Check Exercises (2) Consider the following program and answer the questions below: // This is an exercise public class SimpleProgram { public static void main (String[] args) System.out.println (“5 multiplied by 10 is ” + 50); System.out.println (“5 * 10 = “ + “(5 * 10)” ); } 1 2 3 4 5 6 7 8 9 What is the class name in this program? How many methods are there in this class? What are their names? Identify the comments Identify the strings W2.1 Program Structure

Self-Check Exercises (3) Detect the errors in the program below: (5 errors) /* This is an exercise public IncorrectProgram { public void Main (String args) System.out.println (“I study Java ” + “one”) System.out.println (“3 + 7 = + (7 * 3) ); } 1 2 3 4 5 6 7 8 W2.1 Program Structure