Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for some quick and useful notes on these topics: • Conditionals and Loop

ID: 3836790 • Letter: L

Question

Looking for some quick and useful notes on these topics:

• Conditionals and Loops

o The if Statement and Conditions: use if statements, understand and use boolean

expressions and logical operators.

o Other Conditional Statements: understand and use if-else statements, the conditional

operator, nested if-else statements and switch statements.

o Comparing Data: be able to compare primitive data types, objects and strings properly.

o The while Statement: understand and use while loops, nested loops. Recognize infinite loops

o Iterators: use the Scanner class to read files and divide sentences into words.

o Other Repetition Statements: understand and use for loops and do-while loops.

• Object-Oriented Design

o Identifying Classes and Objects

o Class Relationships: understand and use dependencies and aggregation relationships

to communicate between classes. Understand and use the “this” reference. Understand

how to use Abstract classes.

o Interfaces: understand the concept of interfaces and be able to implement them in classes.

o Method Design: be able to decompose methods into smaller easier to manage methods.

Understand parameters passing in Java. Use method overloading and constructor

overloading correctly when needed.

Explanation / Answer

Conditionals and Loops:

Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.

Java IF Statement

The Java if statement tests the condition. It executes the if block if condition is true.

Syntax:

if(condition){  

//code to be executed  

}

Java IF-else Statement

The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax:

if(condition){  

//code if condition is true  

}else{  

//code if condition is false  

}

Java IF-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

if(condition1){  

//code to be executed if condition1 is true  

}else if(condition2){  

//code to be executed if condition2 is true  

}  

else if(condition3){  

//code to be executed if condition3 is true  

}  

...  

else{  

//code to be executed if all the conditions are false  

}  

Java Switch Statement

The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement.

Syntax:

switch(expression){    

case value1:    

//code to be executed;    

break;  //optional  

case value2:    

//code to be executed;    

break;  //optional  

......    

    

default:     

code to be executed if all cases are not matched;    

}   

Java While Loop

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

Syntax:

while(condition){  

//code to be executed  

}

Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after loop body.

Syntax:

do{  

//code to be executed  

}while(condition);

Boolean Expressions:

Boolean values are values that evaluate to either true or false, and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".

  comparative operators in Java:

·         > : Greater than

·         < : Less than

·         >= : Greater than or equal to

·         <= : Less than or equal to

·         == : Equal to

·         != : Not equal to

boolean operators in Java:

·         ! : Boolean NOT

·         && : Boolean AND

·         || : Boolean inclusive OR

·         ^ : Boolean exclusive XOR

primitive data types:

The eight primitive data types in Java are:

Values of class type are references. Strings are references to an instance of class String.

Object :

Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

Strings

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Creating Strings

The most direct way to create a string is to write:

In this case, "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.

As with any other object, you can create String objects by using the new keyword and a constructor. The String class has thirteen constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:

The String class is immutable, so that once it is created a String object cannot be .

Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:

1.    public boolean hasNext() it returns true if iterator has more elements.

2.    public object next() it returns the element and moves the cursor pointer to the next element.

3.    public void remove() it removes the last elements returned by the iterator. It is rarely used.

Scanner class:

Object-Oriented Design:

Object:

Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

Class:

Collection of objects is called class. It is a logical entity.

Inheritance:

When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism:

When one task is performed by different ways i.e. known as polymorphism. For example: to convince the customer differently, to draw something e.g. shape or rectangle etc.

In java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Abstraction

Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.

In java, we use abstract class and interface to achieve abstraction.

Encapsulation:

Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Aggregation:

If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.

Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.

1.      class Employee{  

2.      int id;  

3.      String name;  

4.      Address address;//Address is a class  

5.      ...  

6.      }  

Abstract Class:

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

this:

Interface:

An interface in java is a blueprint of a class. It has static constants and abstract methods.

The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java.

Java Interface also represents IS-A relationship.

It cannot be instantiated just like abstract class.

Constructor:

Constructor in java is a special type of method that is used to initialize the object.

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote