Introduction to Java

Java

Introduction to Java

Objective: Conceptual Design documentation What is Java? Installing the IDE What is Java? Java is a popular programming langauge that is currently owned by Oracle and runs on over 3 billion devices. It is used on mobile,desktop & web applications. Some platforms Java works on are as of follows: Windows, Mac, Linux. It is a free & open-source. Java is also similar to both c# and c++. Conceptual Design documentation Understand some abbreviations like JDK, java SE, EE, ME, JavaFX, JRE,JVM, API, SWING, GUI https://www.oracle.com/java/technologies/platform-glance.html download & Install Java (JDK) and NetBeans Download JDK most recent version: https://www.oracle.com/java/technologies/javase-downloads.html Download NetBeans: https://netbeans.org/downloads/8.2/rc/

Installing an IDE(Integrated Development Environment) & Online Java Compiler|Runner

We will be installing Netbeans or Eclipse as our IDE. An application that allows us to write java, compile it and run it to check for errors in the code. Above is a video, for assistance to the installation, Below the video is images of the IDE, and the installation wizards.

Link to Netbeans: https://netbeans.org/ (Can be downloaded as Compressed file[ZIP] and extracted to get the .exe/wizard to run)

Link to Eclipse: https://www.eclipse.org/ide/

Both IDEs have their own pros and cons.

One Pro Eclipse has is the ability to add in plug-ins to the IDE.

Before you can install the IDE, you would need a Java SE Development Kit, Which you can download from this link: Note Only Download the Versions compatible with your system.

https://www.oracle.com/java/technologies/javase-downloads.html

Online Java Compiler: https://www.jdoodle.com/online-java-compiler/

Write a simple program

Objective: write simple program to display message and another program to display result of mathematical computation.

Description:

1st program: display the message Welcome to lecturus.org! & perform mathematical computations(2*2.5)and display the result.

  • Comments: are preceded by two slashes (//) on a line, called a line comment or enclosed between /* and */ on one or several lines, called a block comment
  • Class: is a blueprint or a set of instructions to build a specific type of object
  • main method: where the program begins executed, it may contain several methods

  • System.out.println(“”):   displays string (a sequence of characters) statementmust be enclosed in double quotation marks

  • a block that groups the program’s components. each block begins with an opening brace ({) and ends with a closing brace (}).

    • Every class has a class block that groups the data and methods of the class.
    • Every method has a method block that groups the statements in the method.

      Blocks can be nested, meaning that one block can be placed within another,

  • Reserved words, or keywords: it has specific meaning to the compiler and can’t be use in any other purpose like  class- void- public – static

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 marks

Enclose a string (sequence of characters).

;

Semicolon

Mark the end of a statement.

Finding Errors

programming Errors:

Syntax Errors

Syntax errors result from errors in code construction

 

Runtime Errors

Runtime errors are errors that cause a program to terminate abnormally

 

Logic Errors

when a program does not perform the way, it was intended to.

 

Common Errors

Missing a closing brace, missing a semicolon, missing quotation marks for strings, and mis- spelling names

Variables, numeric data Types, Numeric & Exponent Operators

Reading Input from the Console & Augmented Assignment Operators

  • Reading Input from the Console

Java uses System.out to refer to the standard output device and System.in to the standard input device

use the Scanner class to create an object to read input from System.in

Scanner input = new Scanner(System.in);

  • Reading Numbers from the Keyboard

the nextDouble() method in the Scanner class to read a double value from the keyboard. You can also use the methods listed in Table 2.2 to read a number of the byte, short, int, long, and float type.

 nextByte()
 nextShort()
 nextInt()
 nextLong()
 nextFloat()
 nextDouble()
  • Augmented Assignment Operators
 

Very often the current value of a variable is used, modified, and then reassigned back to the same variable. For example, the following statement increases the variable count by 1:

count = count + 1;

Java allows you to combine assignment and addition operators using an augmented (or

compound) assignment operator. For example, the preceding statement can be written as

count += 1;

Boolean Data Type & if Statements

  • Boolean is a data type that goes in true or false.

boolean variable = true(returns true)

boolean variable = false(returns false)

boolean user = true;

if ( user == true) {
System.out.println(“it’s true”);
}
else {
System.out.println(“it’s false”);
}

  • boolean expressions such as

x = 10; y = 9

system.out.println(x>y);

which will return a value of true.

  • If statements support less than, less than equals, greater than, greater than equals, equal to and not equals to. 

You would use if statements during the case that the condition runs true.

For Else statements, it would be the opposite.

Else if would be used if there are other conditions to test out.

Switch

Switch-case would be used if there are multiple conditions + blocks to utilize.

if (condition) {
// block of code to be executed if the condition is true
}

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

for switch you would have to use a variable with the case being the value.

switch (grade){
case ‘A’:
System.out.println(“conrgrats you pass With A grade “);
break;
case ‘B’:
System.out.println(“conrgrats you pass with B grade “);
break;

case ‘C’:
System.out.println(“conrgrats you pass with C grade “);
break;

case ‘F’:
System.out.println(“sorry you fail “);
break;

default:
System.out.println(“sorry cant determin either you pass or fail “);

}

Default is if there’s no case that fits the expression.

While loop - do while loop

Loops

A loop can be used to tell a program to execute statements repeatedly.

Suppose that you need to display a string (e.g., Welcome to Lecturus!) a hundred times. It

System.out.println(“Welcome to Lecturus!”);

System.out.println(“Welcome to Lecturus!”);
System.out.println(“Welcome to Lecturus!”);

….

….

100 times !!!!!

Instead we can write code as following:

int count = 0;
while (count < 100) {

System.out.println(“Welcome to Lecturus!”);

count++; }

 

there are three types of loop statements:      

while loops

dowhile loops

and for loops.

 

While loop

A while loop executes statements repeatedly while the condition is true.

 

Here is example to help understand how a loop works.

int sum = 0, i = 1;

while (i < 10)

 { sum = sum + i;

i++; }

System.out.println(“sum is ” + sum); // sum is 45

What happens if the loop is mistakenly written as follows?

int sum = 0, i = 1;

while (i < 10)

{ sum = sum + i;

}

This loop is infinite, because i is always 1 and i < 10 will always be true.

 

The do-while Loop

A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition.

 

do {
// Loop body; Statement(s);

} while (loop-continuation-condition);

For Loop

For loop

A for loop has a concise syntax for writing loops.

A for loop can be used to simplify the preceding loop as:

for (i = initialValue; i < endValue; i++) // Loop body

}

In general, the syntax of a for loop is:

for (initial-action; loop-continuation-condition; action-after-each-iteration) {

// Loop body;

    Statement(s);

  }

for loop prints Welcome to Lecturus! a hundred times:

int i;
for (i = 0; i < 100; i++) {

System.out.println(“Welcome to Lecturus!”); }

Int sum=0;

For(int count=1;count<10;count++){

Sum+=count;}

System.out.println(sum);

Methods

Methods

Methods can be used to define reusable code and organize and simplify coding.

you need to find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively. You may write the code as follows:

int sum = 0;
for (int i = 1; i <= 10; i++)

sum += i;
System.out.println(“Sum from 1 to 10 is ” + sum);

sum = 0;
for (int i = 20; i <= 37; i++)

sum += i;
System.out.println(“Sum from 20 to 37 is ” + sum);

sum = 0;
for (int i = 35; i <= 49; i++)

sum += i;
System.out.println(“Sum from 35 to 49 is ” + sum);

 

public static int sum(int i1, int i2)

 { int result = 0;


for (int i = i1; i <= i2; i++)

result += i;

return result;

}

public static void main(String[] args) {

 System.out.println(“Sum from 1 to 10 is ” + sum(1, 10));

System.out.println(“Sum from 20 to 37 is ” + sum(20, 37));

System.out.println(“Sum from 35 to 49 is ” + sum(35, 49));

}

 

 method definition

consists of its method name, parameters, return value type, and body.

 

Calling a Method

To execute the method, you have to call or invoke it. There are two ways to call a method, depending on whether the method returns a value or not.

If a method returns a value, a call to the method is usually treated as a value. For example,

int larger = max(3, 4);


calls max(3, 4) and assigns the result of the method to the variable larger.

Another example of a call that is treated as a value is

System.out.println(max(3, 4));

which prints the return value of the method call max(3, 4).
If a method returns void, a call to the method must be a statement. For example, the

method println returns void. The following call is a statement: System.out.println(“Welcome to lecturs!”);

Single Dimensional Array (Part: 1)

Single dimensional array arrays

Suppose, for instance, that you need to read 100 numbers, compute their average, and find out how many numbers are above the average. Your program first reads the numbers and computes their average, then compares each number with the average to determine whether it is above the average. In order to accomplish this task, the numbers must all be stored in variables. You have to declare 100 variables and repeatedly write almost identical code 100 times. Writing a program this way would be impractical. So, how do you solve this problem?

Java and most other high-level languages pro- vide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. In the present case, you can store all 100 numbers into an array and access them through a single array variable.

Array Basics

Once an array is created, its size is fixed. An array reference variable is used to access the elements in an array using an index.

 

Creating Arrays

Unlike declarations for primitive data type variables, the declaration of an array variable does not allocate any space in memory for the array. It creates only a storage location for the refer- ence to an array. If a variable does not contain a reference to an array, the value of the variable is null. You cannot assign elements to an array unless it has already been created , after an array variable is declared, you can create an array by using the newoperator and assign its reference to the variable with the following syntax:

arrayRefVar = new elementType[arraySize];

arrayRefVar = new elementType[arraySize];

This statement does two things: (1) it creates an array using new elementType[arraySize]; (2) it assigns the reference of the newly created array to the variable arrayRefVar.

Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement as:

elementType[] arrayRefVar = new elementType[arraySize]; or

elementType arrayRefVar[] = new elementType[arraySize]; Here is an example of such a statement:

double[] myList = new double[10];

Displaying arrays: To print an array, you have to print each element in the array using a loop like the following:

for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + ” “);

}

Single Dimensional Array (Part: 2)

Example:

public class AnalyzeNumbers {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print(“Enter the number of items: “);

int n = input.nextInt();

 double [] numbers = new double[n];

double sum = 0;

System.out.print(“Enter the numbers: “);

for (int i = 0; i < n; i++) {

numbers[i] = input.nextDouble(); sum += numbers[i];

}

double average = sum / n;

System.out.println(“Average is ” + average);

 

}

}

Enter the number of items: 10

Enter the numbers: 3.4 5 6 1 6.5 7.8 3.5 8.5 6.3 9.5 

Average is 5.75

Conclusion

This course was designed to teach basics in Java programming. Its intended for those who have no clue about programming  and who wish to gain some knowledge. Thank you for your interest in this introductory Java programming  course. If you’d like to learn more I suggest you to read the following book:

Intro to Java Programming, Comprehensive Version (10th Edition)

Prof. Gonzalez At New York City College of Technologu and Founder of Lecturus.org and Lecturus Global

Ahmed Sherif

Instructor AND Web Developer Programming Data Engineer

Justin Choi

Data Engineer and Web Developer convert raw data ETL SSAS

Content Developer Justin Choi

Lightbulb logo
Lecturus

Lecturus is a platform that offers training to individuals interested in developing or enhancing their computer skills, as well as a career change or advancement.

Get In Touch

147 Prince St, Brooklyn, NY 11201

Lightbulb logo
Lecturus

Lecturus is a platform that offers training to individuals interested in developing or enhancing their computer skills, as well as a career change or advancement.

Get In Touch

147 Prince St, Brooklyn, NY 11201

Scroll to Top