Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Chapter 1 - Introduction To Computers, Programs, and Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chapter 1 - Introduction To Computers, Programs, and Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

A Simple Java Program

A Java program is executed from the main method in the class.


Welcome.java

1 public class Welcome {


2 public static void main(String[] args) {
3 // Display message Welcome to Java! on the console
4 System.out.println("Welcome to Java!");
5 }
6 }
Classes naming conventions
Class names should be nouns in UpperCamelCase (in mixed case with the
first letter of each internal word capitalized). Try to keep your class names
simple and descriptive.

Example:
• class Employee

• class Student

• class AddTwoNumber
Variables naming conventions
The variable name should start with a lowercase letter. Parameter names,
member variable names, and local variable names should be written
in lowerCamelCase.

Example:

• firstName

• lastName

• sum
Comment
• comment that documents what the program is and how it is
constructed. Comments help programmers to communicate and
understand the program.
• They are not programming statements and thus are ignored by the
compiler.
• Line comment:- In Java, comments are preceded by two slashes (//)
on a line, called a line comment.
• Block comment:- enclosed between /* and */ on one or several
lines, called a block comment or paragraph comment.
• When the compiler sees //, it ignores all text after // on the same line.
When it sees /*, it scans for the next */ and ignores any text
between /* and */.
• Here are examples of comments:

// This application program displays Welcome to Java!

/* This application program displays Welcome to Java! */

/* This application program

displays Welcome to Java! */

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}
Special Characters
Character Name Description

{} Opening and closing braces Denote a block to enclose statements.

() Opening and closing parentheses Used with methods.

[] Opening and closing brackets Denote an array.

// Double slashes Precede a comment line.

"" Opening and closing quotation Enclose a string (i.e., sequence of


marks characters).

; Semicolon Mark the end of a statement.


• syntax rules:- If your program violates a rule—for example, if the semicolon
is missing, a brace is missing, a quotation mark is missing, or a word is misspelled
—the Java compiler will report syntax errors. Try to compile the program with
these errors and see what the compiler reports.
WelcomeWithThreeMessages.java

1 public class WelcomeWithThreeMessages {

2 public static void main(String[] args) {

3 System.out.println("Programming is fun!");

4 System.out.println("Fundamentals First");

5 System.out.println("Problem Driven");

6 }

7 }
• Further, you can perform mathematical computations and display the result on
the console.
• gives an example of evaluating
ComputeExpression.java
1 public class ComputeExpression {
2 public static void main(String[] args) {
3 System.out.println((10.5 + 2 * 3) / (45 – 3.5));
4 }
5 }

OutPut:
0.39759036144578314
System.out.println
• Two ways to use System.out.println :
• System.out.println("text");
Prints the given message as output.

• System.out.println();
Prints a blank line of output.
Programming Errors
• Programming errors can be categorized into three types: syntax errors, runtime
errors, and logic errors.

• Syntax Errors:- Errors that are detected by the compiler are called syntax errors
or compile errors. Syntax errors result from errors in code construction, such as
mistyping a keyword, omitting some necessary punctuation, or using an opening
brace without a corresponding closing brace. These errors are usually easy to
detect because the compiler tells you where they are and what caused them.
Syntax
• syntax: The set of legal structures and commands that can be
used in a particular language.
• The “spelling” and “grammar” of a programming language.
• Every basic Java statement ends with a semicolon ;
• The contents of a class or method occur between { and }

• syntax error (compiler error): A problem in the structure of a


program that causes the compiler to fail.
• Missing semicolon
• Too many or too few { } braces
• Class and file names do not match
• ...
ShowSyntaxErrors.java
1 public class ShowSyntaxErrors {
2 public static main(String[] args) {
3 System.out.println("Welcome to Java);
4 }
5 }

program actually has two errors:

■ The keyword void is missing before main in line 2.

■ The string Welcome to Java should be closed with a closing quotation mark
in line 3.
Syntax error example
1 public class Hello {
2 pooblic static void main(String[] args) {
3 System.owt.println("Hello, world!")_
4 }
5 }

• Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors

• The compiler shows the line number where it found the error.
More on syntax errors
• Java is case-sensitive
• Hello and hello are not the same

1 Public class Hello {


2 public static void main(String[] args) {
3 System.out.println("Hello, world!");
4 }
5 } compiler output:

Hello.java:1: class, interface, or enum expected


Public class Hello {
^
1 error
Runtime Errors
• Runtime errors : are errors that cause a program to terminate abnormally. They
occur while a program is running if the environment detects an operation that is
impossible to carry out. Input mistakes typically cause runtime errors. An input
error occurs when the program is waiting for the user to enter a value, but the
user enters a value that the program cannot handle. For instance, if the program
expects to read in a number, but instead the user enters a string, this causes data-
type errors to occur in the program.

• Another example of runtime errors is division by zero. This happens when the
divisor is zero for integer divisions.
ShowRuntimeErrors.java
1 public class ShowRuntimeErrors {

2 public static void main(String[] args) {

3 System.out.println(1 / 0);

4 }

5 }

The runtime error causes the program to terminate abnormally.


Logic Errors
• Logic errors occur when a program does not perform the way it was intended to. Errors of
this kind occur for many different reasons. For example, suppose you wrote the program to
convert Celsius 35 degrees to a Fahrenheit degree:
ShowLogicErrors.java
1 public class ShowLogicErrors {
2 public static void main(String[] args) {
3 System.out.println("Celsius 35 is Fahrenheit degree ");
4 System.out.println((9 / 5) * 35 + 32);
5 }
6 }
Output:
Celsius 35 is Fahrenheit degree
• You will get Fahrenheit 67 degrees, which is wrong. It should be 95.0.
In Java, the division for integers is the quotient—the fractional part is
truncated—so in Java 9 / 5 is 1. To get the correct result, you need to
use 9.0 / 5, which results in 1.8.
Common Errors
Missing a closing brace, missing a semicolon, missing quotation marks for strings,
and misspelling names are common errors for new programmers.

Common Error 1: Missing Braces

public class Welcome {

}  Type this closing brace right away to match the opening brace
Common Error 2: Missing Semicolons

public static void main(String[] args) {

System.out.println("Programming is fun!");

System.out.println("Fundamentals First");

System.out.println("Problem Driven")  Missing a semicolon


}
Common Error 3: Missing Quotation Marks

public static void main(String[] args) {

System.out.println("Programming is fun!");

System.out.println("Fundamentals First");

System.out.println("Problem Driven );
}

Missing a quotation mark


Common Error 4: Misspelling Names
• Java is case sensitive. Misspelling names is a common error for new
programmers. For example, the word main is misspelled as Main and String is
misspelled as string in the following code.

1 public class Test {

2 public static void Main(string[] args) {

3 System.out.println((10.5 + 2 * 3) / (45 – 3.5));

4 }

5 }

You might also like