❤5
Forwarded from 𝒔𝒂𝒓𝒂.
❤3
Forwarded from Badr Sh
Sanfoundry
Constructors - Java Questions & Answers - Sanfoundry
This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Constructor”. 1. What is true about private constructor? a) Private constructor ensures only one instance of a class exist at any point of time b) Private constructor ensures multiple…
❤5
Forwarded from Badr Sh
❤4
#EC252
[Lecture 3]
• variables defined within a method (beside the parameters) are called helper variables.
• String methods:
.charAt();
return type: char.
function: returns the character at the specified index.
last character of a word:
word.charAt(word.length()-1);
.equals();
return type: boolean.
function: compares two strings. returns true if the strings are equal, and false if not.
.indexOf();
return type: int.
function: returns the position of the first found occurrence of specified characters in a string.
(when not found returns -1)
.length();
return type: int.
function: returns the length of a specified string.
.substring();
return type: String.
function: returns a new string which is the substring of a specified string.
when given one parameter:
substring ( index )
returns:
from index → end of original string.
when given two parameters:
returns:
string [index, index)
• Scanner methods:
.next();
reads a string from the keyboard up to the first encountered space.
.nextLine();
reads up to the a line break.
.nextInt();
reads an integer from the keyboard
• Object: something that has methods and a value.
• Integers (variables of type int) have no methods therefore are not objects.
• objects must be created with the new command, strings make an exception to this rule, using the new command when creating a String object is uncommon.
• Class: an object's type.
• Arraylists are the most used object-containers in java.
• Arraylists can be used to store unlimited number of objects.
• creating an arraylist from an array:
String[] array;
ArrayList<string> list = new ArrayList<>(Arrays.asList(array);
• creating an array from an arraylist:
String[] array1 = new String [list.size()];
list.toArray(array1);
• static max and min in the java.util.Collections class return the maximum and minimum element in an arraylist.
java.util.Collections.max(list)
java.util.Collections.min(list)
• we can use a for-each loop to go through the items of an ArrayList.
• ordering a string type arraylist by size means putting it in alphabetical order.
• ordering, reversing and shuffling an arraylist:
Collections.sort(arraylist);
Collections.reverse(arraylist);
Collections.shuffle(arraylist);
• an integer type arraylist is defined as ArrayList<Integer>
• when dealing with integer type arraylists,
arraylist.remove(4);
// removes number with index 4
arraylist.remove(Integer.valueOf(4));
// removes number 4 from arraylist
• truth values (boolean type variables)
if (boolean)
// is the same as boolean==true
if (!boolean)
// is the same as boolean==false
[Lecture 3]
• variables defined within a method (beside the parameters) are called helper variables.
• String methods:
.charAt();
return type: char.
function: returns the character at the specified index.
last character of a word:
word.charAt(word.length()-1);
.equals();
return type: boolean.
function: compares two strings. returns true if the strings are equal, and false if not.
.indexOf();
return type: int.
function: returns the position of the first found occurrence of specified characters in a string.
(when not found returns -1)
.length();
return type: int.
function: returns the length of a specified string.
.substring();
return type: String.
function: returns a new string which is the substring of a specified string.
when given one parameter:
substring ( index )
returns:
from index → end of original string.
when given two parameters:
returns:
string [index, index)
• Scanner methods:
.next();
reads a string from the keyboard up to the first encountered space.
.nextLine();
reads up to the a line break.
.nextInt();
reads an integer from the keyboard
• Object: something that has methods and a value.
• Integers (variables of type int) have no methods therefore are not objects.
• objects must be created with the new command, strings make an exception to this rule, using the new command when creating a String object is uncommon.
• Class: an object's type.
• Arraylists are the most used object-containers in java.
• Arraylists can be used to store unlimited number of objects.
• creating an arraylist from an array:
String[] array;
ArrayList<string> list = new ArrayList<>(Arrays.asList(array);
• creating an array from an arraylist:
String[] array1 = new String [list.size()];
list.toArray(array1);
• static max and min in the java.util.Collections class return the maximum and minimum element in an arraylist.
java.util.Collections.max(list)
java.util.Collections.min(list)
• we can use a for-each loop to go through the items of an ArrayList.
• ordering a string type arraylist by size means putting it in alphabetical order.
• ordering, reversing and shuffling an arraylist:
Collections.sort(arraylist);
Collections.reverse(arraylist);
Collections.shuffle(arraylist);
• an integer type arraylist is defined as ArrayList<Integer>
• when dealing with integer type arraylists,
arraylist.remove(4);
// removes number with index 4
arraylist.remove(Integer.valueOf(4));
// removes number 4 from arraylist
• truth values (boolean type variables)
if (boolean)
// is the same as boolean==true
if (!boolean)
// is the same as boolean==false
❤12👏2
#EC252
[Lecture 4]
•the method's header consists of:
-modifiers (public, static, etc)
-return value type (int, string, void, etc)
-method's name
-parameter list (formal parameters)
• a method's "signature" is its name and formal parameters
• when invoking a method, the variables passed to it are called "actual parameters" or "arguments"
• in some programming languages:
a value-returning method is called a function
a void method is called a procedure
• we say "define a method" but "declare a variable" because declaration usually involves allocating memory to store data for the declared item
• the only way to give information to a method is through parameters
• call by value: when a variable is passed to a method through its parameters, the method uses a copy of the variable not the original variable. because the parameters in methods are different variables than the variables introduced in the method call. primitive data types follow this rule (int, double, float, char, boolean, short, long, byte).
• call by reference: ArrayLists as parameters are an exception to the rule mentioned above because they are reference data types. when a method is passed an ArrayList as a parameter, it gains access to the original Arraylist, and any changes made by the method will be made to the original Arraylist.
• an object contains a related group of "information" (values) and "functionality" (methods).
• objects are instances of classes.
• the type of a certain group of objects is called a class.
• a class defines what method its objects have and attributes.
[Lecture 4]
•the method's header consists of:
-modifiers (public, static, etc)
-return value type (int, string, void, etc)
-method's name
-parameter list (formal parameters)
• a method's "signature" is its name and formal parameters
• when invoking a method, the variables passed to it are called "actual parameters" or "arguments"
• in some programming languages:
a value-returning method is called a function
a void method is called a procedure
• we say "define a method" but "declare a variable" because declaration usually involves allocating memory to store data for the declared item
• the only way to give information to a method is through parameters
• call by value: when a variable is passed to a method through its parameters, the method uses a copy of the variable not the original variable. because the parameters in methods are different variables than the variables introduced in the method call. primitive data types follow this rule (int, double, float, char, boolean, short, long, byte).
• call by reference: ArrayLists as parameters are an exception to the rule mentioned above because they are reference data types. when a method is passed an ArrayList as a parameter, it gains access to the original Arraylist, and any changes made by the method will be made to the original Arraylist.
• an object contains a related group of "information" (values) and "functionality" (methods).
• objects are instances of classes.
• the type of a certain group of objects is called a class.
• a class defines what method its objects have and attributes.
❤13👏2💯2
#EC252
تعريف وفائدة الـ signature.
In Java, a signature refers to the unique identifier of a method or constructor. It consists of the method's name and the types of its parameters in the order in which they appear. The return type of the method is not part of its signature.
For example, consider the following method:
The signature of this method is "add(int, int)".
Java uses method signatures to determine which method to call when there are multiple methods with the same name in a class. This is known as method overloading. If two methods have the same name but different signatures, Java can determine which method to call based on the arguments passed to it.
تعريف وفائدة الـ signature.
In Java, a signature refers to the unique identifier of a method or constructor. It consists of the method's name and the types of its parameters in the order in which they appear. The return type of the method is not part of its signature.
For example, consider the following method:
public int add(int a, int b) {
return a + b;
}
The signature of this method is "add(int, int)".
Java uses method signatures to determine which method to call when there are multiple methods with the same name in a class. This is known as method overloading. If two methods have the same name but different signatures, Java can determine which method to call based on the arguments passed to it.
❤8
https://youtube.com/playlist?list=PL59LTecnGM1MMSBPiLN4aMwM7DcnVld0s
playlist فيها برامج تقدروا تدربوا عليها
#EC252
playlist فيها برامج تقدروا تدربوا عليها
#EC252
❤2👍1
#EC252
[Lecture 5]
the
https://www.javatpoint.com/understanding-toString()-method
[Lecture 5]
the
toString() method and how it's usedhttps://www.javatpoint.com/understanding-toString()-method