Computer Programming 2 Lab(1) I.Fatimah Alzahrani.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Getting Started with Java
 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.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Chapter 2: Introduction to C++.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 3b Standard Input and Output Sample Development.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Java Applications & Program Design
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
The Java Programming Language
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Input, Output, and Processing
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
By Mr. Muhammad Pervez Akhtar
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Variables in Java A variable holds either
Topics Designing a Program Input, Processing, and Output
Java Programming: From Problem Analysis to Program Design, 4e
CSS 161 Fundamentals of Computing Introduction to Computers & Java
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Statements and expressions - java
Chapter 2: Introduction to C++.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Getting Started with Java
Presentation transcript:

Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Program Components The first sample application program Ch2Sample1 is composed of three parts: comment, import statement, and class declaration. These three parts are included universally in Java program.

Comments In addition to the instructions for computers to follow, programs contain comments Comments can explain the purpose of the program Comments can explain the meaning of code Comments can provide any other descriptions to help programmers understand the program

A comment is any sequence of text that begins with the marker /* and terminates with another marker */.The beginning and ending comment markers are matched in pairs. You cannot put a comment inside another comment. Another marker for a comment is double slashes //. This marker is used for a single-line comment marker. Any text between the double-slash marker and the end of a line is a comment. Example: Multi line comment : /* */ Single Line comment : //

The third type of comment is called a javadoc comment. It is a specialized Comment that can appear before the class declaration and other program elements. Comments are intended for the programmers only and are ignored by the computer. Therefore, comments are really not necessary in making a program executable, but they are an important aspect of documenting the program. It is not enough to write a program that executes correctly. We need to document the program, and commenting the program is an important part of program documentation.

Import Statement We develop object-oriented programs by using predefined classes, both system and programmer- defined. In Java, classes are grouped into packages, and the Java system comes with numerous packages. We also can logically group our own classes into a package so they can be reused conveniently by other programs. To use a class from a package, we refer to the class in our program by using the following format:

Which we read as “dorm dot Resident.” This notation is called dot notation

A package can include subpackages, forming a hierarchy of packages. In referring to a class in a deeply nested package, we use multiple dots. For example, we write: Javax.swing.JFrame;

Note: add import statement at the beginning of the program, and import statement is terminated by a semicolon. If we need to import more than one class from the same package, then instead of using an import statement for every class, we can import them all by using asterisk notation *.

Class Declaration A Java program is composed of one or more classes; some are predefined classes, while others are defined by us. To define a new class, we must declare it in the program, or make a class declaration. The syntax for declaring the class is

Where is the name of the class and is a sequence of class member declarations. The word class is a reserved word used to mark the beginning of a class declaration. A class member is either a data value or a method. We can use any valid identifier that is not reserved to name the class.

Note: one of the classes in a program must be designated as the main class. If we designate a class as the main class, then we must define a method called main, because when a Java program is executed, the main method of a main class is executed first. To define a method, we must declare it in a class.

Method Declaration Where is a sequence of terms designating different kinds of methods, is the type of data value returned by a method, is the name of a method, is a sequence of values passed to a method, and is a sequence of instructions. Here’s the method declaration for the main method

Object Declaration Every object we use in a program must be declared. An object declaration designates the name of an object and the class to which the object belongs. Its syntax is

The first declaration declares an Account object named checking, and the second declaration declares three Customer objects. To declare an object as an instance of some class, the class must be defined already.

Identifier Any valid identifier that is not reserved for other uses can be used as an object name. A Java identifier is a sequence of letters, digits, underscores (_), and dollar signs ($) with the first one being a letter. We use an identifier to name a class, object, method, and others.

The following words are all valid identifiers:

Reserved word It is an identifier that is used for a specific purpose and cannot be used for any other purpose, such as for the name of an object, class, method and variables. Standard naming convention for Java

A Program Template for Simple Java Applications The following diagram shows a program template for simple Java applications. You can follow this program template to write very simple Java applications. The structure of the sample program Ch2Sample1 follows this template.

Sample Java Standard Classes Eventually, you must learn how to define your own classes, the classes you will reuse in writing programs. But before you can become adept at defining your own classes, you must learn how to use existing classes.

Standard Output When a program computes a result, we need a way to display this result to the user of the program. We output data such as the computation results or messages to the console window (Standard Output Window) via System.out We use the print() method to output a value. For example, executing the cod System.out.print("Hello, Dr. Caffeine.");

Notice that they all appear on the same line. If we want them to appear on individual lines, we can use the println() method instead of print

Standard Input We have System.in for input. We call the technique to input data using System.in standard input. System.in accepts input from the keyboard. Scanner Class The Scanner class from the java.util package provides a necessary input facility to accommodate various input routines To input data from the standard input by using a Scanner object, we first create it by passing System.in

Once we have a Scanner object, then we can input a single word by using its next() method. Here’s code to input the first name of a person:

Now let’s consider the case in which we want to input both the first name and the last name. We can follow the sample code and input them one by one as follows:

Exercise: Write a programme to input both the first name and the last name. *Using nextLine() to input String method

Output:

Getting Numerical Input (Scanner Class) *To input an int value, we use the nextInt() method. Here’s an example of inputting a person’s age

In addition to the int data type, we have five input methods that correspond to the other numerical data types

Note: The nextDouble() method accepts the value 35 and then converts it to a double data type. The method returns a double value, so even if the user enters an integer, you cannot assign the input to an int variable. See the following codes

Valid Invalid

Input String and numerical value Now let’s study how we can mix the input of strings and numerical values. Consider inputting a horse’s name and age.

Note: Everything seems to be working okay. What will happen if the name of a horse has more than one word, such as Sea Biscuit? The code will not work

Because only the first word is assigned to the String variable horseName. Remember that the default delimiter is the white space, so the blank space after the first word is treated as the end of the first input. The Solution you can use nextLine() method to input String.

Converting String number to Specific data type We can use class method parseInt( ) of the Integer class to convert a string to an int. Other common conversion methods are parseDouble, parseFloat, and parseLong of the Double, Float, and Long classes, respectively. Example : converts "14" to an int value 14 Note: Passing a string that cannot be converted to an int (e.g., "12b") will result in an error.

Example: 1.Write a code to input the height of a user in meter (int) and centimetre (float). 2. Write a code to input the full name of a person and his or her age. The full name of a person includes the first name and the last name

Reference (BOOK) : A Comprehensive Introduction to Object- Oriented Programming with Java, C.Thomas Wu