28 April 2023 07:31: CS2 Page 3
28 April 2023 07:31: CS2 Page 3
CS2 Page 3
CS2 Page 4
CS2 Page 5
CS2 Page 6
CS2 Page 7
Java
28 April 2023 07:33
CS2 Page 8
CS2 Page 9
CS2 Page 10
CS2 Page 11
CS2 Page 12
CS2 Page 13
CS2 Page 14
CS2 Page 15
CS2 Page 16
CS2 Page 17
CS2 Page 18
CS2 Page 19
Java Program Structure
28 April 2023 07:34
CS2 Page 20
CS2 Page 21
CS2 Page 22
CS2 Page 23
CS2 Page 24
CS2 Page 25
CS2 Page 26
CS2 Page 27
CS2 Page 28
CS2 Page 29
CS2 Page 30
CS2 Page 31
CS2 Page 32
CS2 Page 33
CS2 Page 34
CS2 Page 35
Java Basics
28 April 2023 07:34
CS2 Page 36
CS2 Page 37
CS2 Page 38
CS2 Page 39
CS2 Page 40
CS2 Page 41
CS2 Page 42
CS2 Page 43
CS2 Page 44
CS2 Page 45
CS2 Page 46
CS2 Page 47
CS2 Page 48
CS2 Page 49
CS2 Page 50
Operators and Arithmetic Expressions
28 April 2023 07:34
CS2 Page 51
CS2 Page 52
CS2 Page 53
CS2 Page 54
CS2 Page 55
CS2 Page 56
CS2 Page 57
CS2 Page 58
CS2 Page 59
CS2 Page 60
CS2 Page 61
CS2 Page 62
CS2 Page 63
CS2 Page 64
CS2 Page 65
CS2 Page 66
CS2 Page 67
Decision Making
28 April 2023 07:35
CS2 Page 68
CS2 Page 69
CS2 Page 70
CS2 Page 71
CS2 Page 72
CS2 Page 73
CS2 Page 74
CS2 Page 75
CS2 Page 76
CS2 Page 77
CS2 Page 78
CS2 Page 79
CS2 Page 80
CS2 Page 81
CS2 Page 82
CS2 Page 83
CS2 Page 84
CS2 Page 85
Loops
28 April 2023 07:35
CS2 Page 86
CS2 Page 87
CS2 Page 88
CS2 Page 89
CS2 Page 90
CS2 Page 91
CS2 Page 92
CS2 Page 93
CS2 Page 94
CS2 Page 95
CS2 Page 96
CS2 Page 97
CS2 Page 98
CS2 Page 99
CS2 Page 100
CS2 Page 101
CS2 Page 102
Class
28 April 2023 07:35
Types of Constructors in Java Constructors are a crucial part of the Java programming language, and they are used
Now is the correct time to discuss the types of the constructor, so primarily there are three types of to initialize objects of a class. Here is a brief note on constructors in Java, with
constructors in Java are mentioned below: subheadings:
1. Definition: A constructor in Java is a special type of method that is used to initialize
• No-Argument Constructor
the object's state. It is called when an object of a class is created.
• Parameterized Constructor
2. Naming Convention: The constructor method should have the same name as the
• Default Constructor
class, and it must not have any return type. Java supports overloading of constructors
with different arguments lists.
1. No-Argument Constructor in Java 3. Role in Initialization: The primary role of a constructor is to initialize the instance
variables of an object. The constructor is executed only once when the object is
A constructor that has no parameter is known as the No-argument or Zero argument constructor. If we created, and it is automatically called by the JVM.
don’t define a constructor in a class, then the compiler creates aconstructor(with no arguments) for
the class. And if we write a constructor with no arguments, the compiler does not create a default
constructor.
Example:
• Java
import java.io.*;
class Geek {
int num;
String name;
class GFG {
public static void main(String[] args)
{
// this would invoke default constructor.
Geek geek1 = new Geek();
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields
of the class with our own values, then use a parameterized constructor.
Example:
• Java
import java.io.*;
class Geek {
// constructor with one argument
Geek(String name)
{
System.out.println("Constructor with one "
+ "argument - String : " + name);
}
System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}
class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments
A constructor that has no parameters is known as default the constructor. A default constructor is
invisible. And if we write a constructor with no arguments, the compiler does not create a default
constructor. It is taken out. It is being overloaded and called a parameterized constructor. The default
constructor changed into the parameterized constructor. But Parameterized constructor can’t change
the default constructor.
Example:
• Java
// Driver class
class GFG {
// Default Constructor
GFG() { System.out.println("Default constructor"); }
// Driver function
public static void main(String[] args)
{
GFG hello = new GFG();
}
}
Output
Default constructor
From <https://www.geeksforgeeks.org/constructors-in-java/>
From <https://www.geeksforgeeks.org/constructors-in-java/>
Only wrap the code that may throw an exception inside the try block. Wrapping too much code can
make it difficult to locate the source of the exception.
Catch only the exceptions that you can handle. Catching all exceptions using the Exception class is not a
good practice as it can mask other issues in the code.
Handle the exception appropriately. This may include logging the exception, providing a meaningful
error message to the user, or retrying the operation.
Use finally block if you want to execute some code regardless of whether an exception was thrown or
not. This block executes even if an exception is thrown, and it is used to release resources that were
acquired in the try block.
Here's an example of how to use a try-catch block in Java, as presented in the book:
try {
// code that may throw an exception
}
catch(Exception e) {
// code to handle the exception
}
finally {
// code to release resources
}
In this example, the code inside the try block is the code that may throw an exception. The catch block is
executed if an exception occurs, and the type of the exception is specified in the parentheses after the
catch keyword. The code inside the finally block is executed regardless of whether an exception was
thrown or not, and it is used to release resources that were acquired in the try block.
Syntax:
try {
// code that may throw an exception
}
catch(ExceptionType1 e1) {
// code to handle ExceptionType1
}
catch(ExceptionType2 e2) {
// code to handle ExceptionType2
}
...
catch(ExceptionTypeN eN) {
// code to handle ExceptionTypeN
}
finally {
// optional code to be executed regardless of whether an exception is thrown or not
}
The code within the try block is executed, and if any exception is thrown, it is caught by the
corresponding catch block that matches the type of the exception. Each catch block can handle a specific
type of exception. The finally block is optional and is executed regardless of whether an exception is
thrown or not.
Example:
There are five methods of an applet life cycle, and they are:
• init(): The init() method is the first method to run that initializes the applet. It can be
invoked only once at the time of initialization. The web browser creates the initialized
objects, i.e., the web browser (after checking the security settings) runs the init() method
within the applet.
• start(): The start() method contains the actual code of the applet and starts the applet. It
is invoked immediately after the init() method is invoked. Every time the browser is
loaded or refreshed, the start() method is invoked. It is also invoked whenever the applet
is maximized, restored, or moving from one tab to another in the browser. It is in an
inactive state until the init() method is invoked.
From <https://www.javatpoint.com/applet-life-cycle-in-java>
From <https://chat.openai.com/c/4153b405-765c-4cd3-8abb-67341d5c77cf>
import java.applet.Applet;
import java.awt.*;
Designing a web page with applet and HTML involves creating an HTML page that embeds the Java
applet. Here is a brief guide on how to design a web page with applet and HTML:
1. Create a Java applet that you want to embed in the HTML page.
2. Compile the Java applet code and generate the applet class file.
3. Write the HTML code that embeds the applet using the <applet> tag.
4. Specify the applet class file name and any required parameters within the <applet> tag.
5. Save the HTML file with the desired file name and extension (.html).
6. Run the HTML file in a web browser to view the applet in action.
Here is an example HTML code to embed a Java applet:
<html>
<head>
<title>My Java Applet Example</title>
</head>
<body>
<h1>My Java Applet Example</h1>
<applet code="MyApplet.class" width="300" height="300">
<param name="message" value="Hello World!">
</applet>
</body>
</html>
In the above code, the <applet> tag specifies the applet class file name (MyApplet.class) and the applet's
width and height. The <param> tag within the <applet> tag is used to pass parameters to the applet. In
this example, the message "Hello World!" is passed as a parameter to the applet.
From <https://chat.openai.com/c/4153b405-765c-4cd3-8abb-67341d5c77cf>
From <https://chat.openai.com/c/4153b405-765c-4cd3-8abb-67341d5c77cf>
Retrieve the parameter values in your applet code using the getParameter() method of the Applet
class, for example:
2. Note that the getParameter() method returns a string, so you may need to convert the parameter value
to the desired data type using methods like Integer.parseInt().
By passing parameters to your applet program, you can make it more dynamic and customizable based
on user inputs.