5 mark
4 th
import [Link];
import [Link].*;
public class SimpleDrawing extends Applet
{
public void paint(Graphics g)
{
// Drawing lines
[Link](50, 50, 200, 50); // Draw a line from (50,50) to (200,50)
[Link](200, 50, 200, 150); // Draw a line from (200,50) to (200,150)
[Link](200, 150, 50, 150); // Draw a line from (200,150) to (50,150)
[Link](50, 150, 50, 50); // Draw a line from (50,150) to (50,50)
// Drawing rectangle
[Link](250, 50, 150, 100); // Draw a rectangle with top-left corner at
(250,50) and width=150, height=100
}
}
Html
<html>
<head>
<title>Simple Drawing Applet</title>
</head>
<body>
<applet code="[Link]" width="500" height="300">
</applet>
</body>
</html>
Output
5 mark
3 rd
import [Link];
public class VectorDemo
{
public static void main(String args[])
{
// Creating a vector
ArrayList<Integer> vector = new ArrayList<>();
// Adding elements to the vector
[Link](10);
[Link](20);
[Link](30);
// Displaying elements of the vector
[Link]("Elements of the vector:");
for (Integer num : vector)
{
[Link](num);
}
}
}
Output
Elements of the vector:
10
20
30
5 mark
5 th
Purpose: InputStream is used OutputStream is used
for reading data from for writing data to a
a source. destination.
Data Flow InputStream reads utputStream accepts
Direction: data from a source bytes as input and
and converts it into a writes them to a
stream of bytes. destination.
Abstraction Level: InputStream provide It also provide an
an abstraction for abstraction for reading
reading and writing and writing data at the
data at the byte level. byte level.
Return Values: InputStream returns OutputStream doesn't
-1 when it reaches the return any value upon
end of the data completion of writing
stream. bytes.
Byte Handling: InputStream primarily OutputStream deals
deals with reading with writing bytes to a
bytes from a source destination and
and converting them doesn't handle other
into other data types data types directly.
if necessary.
Examples: FileInputStream, FileOutputStream,
BufferedInputStream, BufferedOutputStream,
and DataInputStream and DataOutputStream
are examples of are examples of
InputStream. OutputStream.
Methods: InputStream provides OutputStream provides
methods like read(), methods like write(int),
read(byte[]), skip(), write(byte[]), flush(),
and close() for and close() for writing
reading data. data.
Directionality: InputStream is OutputStream is also
unidirectional, unidirectional,
supporting only the supporting only the
reading of data from a writing of data to a
source. destination.
Error Handling: Errors during Errors during writing
reading operations operations in
in InputStream are OutputStream are also
typically handled by typically handled by
throwing throwing IOExceptions.
IOExceptions
Usage: InputStream is OutputStream is
commonly used for commonly used for tasks
tasks like file input, like file output, network
network communication, and
communication, and writing serialized data.
parsing serialized
data.
Input
FileOutputStream fileOut = new FileOutputStream (" ARBRDD . txt " ) ;
Output
File is successfully updated today ! !
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class ARBRDD{
public static void main ( String args [] ){
try {
FileOutputStream fileOut
= new FileOutputStream ( "ARBRDD . txt " ) ;
String s = " KOL DHKA " ;
byte b [] = s . getBytes () ;
fileOut . write ( b ) ;
fileOut . close () ;
[Link] ("file is successfully updated today !! " ) ;
}
catch ( Exception e ) {
[Link] ( e ) ;
}
}
}
Output
file is successfully updated today!!
8 mark 4th
8 th 5 th
To create a disk file in Java, you typically follow these steps. Each step
is worth 1 mark:
1. **Choose a File Name**:
- Decide on a name for your file, including the file extension (e.g.,
".txt", ".dat").
2. **Instantiate FileOutputStream**:
- Create an instance of `FileOutputStream`, passing the file name as
a parameter.
3. **Write Data to File**:
- If you want to write data to the file, convert the data to bytes and
use the `write()` method of `FileOutputStream` to write it to the file.
4. **Close the FileOutputStream**:
- Always close the `FileOutputStream` using the `close()` method to
release system resources associated with it.
5. **Handle Exceptions**:
- Handle any potential exceptions that may occur during file
creation, such as `FileNotFoundException` or `IOException`.
6. **Check for File Permissions**:
- Ensure that the directory where you are creating the file has
appropriate permissions to write files.
7. **Specify File Path** (Optional):
- If you want to create the file in a specific directory, specify the full
path of the file when instantiating `FileOutputStream`.
8. **Verify File Creation**:
- After executing your code, verify that the file has been successfully
created in the specified location with the correct content.
Example code demonstrating these steps:
import [Link];
import [Link];
public class CreateDiskFile {
public static void main(String[] args)
{
String fileName = "[Link]"; // Name of the file to create
String data = "Hello, World!"; // Data to write to the file
Try
{
// Step 2: Create a FileOutputStream object
FileOutputStream fileOutputStream = new
FileOutputStream(fileName);
// Step 3: Write data to the file
byte[] bytes = [Link](); // Convert string to bytes
[Link](bytes); // Write bytes to the file
// Step 4: Close the FileOutputStream
[Link]();
[Link]("File created successfully.");
} catch (IOException e) {
// Error handling
[Link]();
}
}
}
Output
File created successfully.
8 mark 1 st
Arithmetic operators in Java are used to perform basic mathematical
operations on numerical data types such as integers and floating-point
numbers. Here are the arithmetic operators in Java:
1. **Addition (+)**:
- Adds two operands.
- Example: `int result = num1 + num2;`
2. **Subtraction (-)**:
- Subtracts the right operand from the left operand.
- Example: `int result = num1 - num2;`
3. **Multiplication (*)**:
- Multiplies two operands.
- Example: `int result = num1 * num2;`
4. **Division (/)**:
- Divides the left operand by the right operand.
- Example: `int result = num1 / num2;`
5. **Modulus (%)**:
- Returns the remainder of the division of the left operand by the
right operand.
- Example: `int result = num1 % num2;`
6. **Increment (++)**:
- Increases the value of the operand by 1.
- Example: `num++;`
7. **Decrement (--)**:
- Decreases the value of the operand by 1.
- Example: `num--;`
These arithmetic operators can be applied to numeric data types such
as int, long, float, and double. They can also be used with variables,
constants, and expressions to perform various mathematical
computations in Java programs.
Example
import [Link].*;
import [Link];
class Operations
{
int a,b;
public void add(int a, int b)
{
int s = a+b;
[Link]("Addition:"+s);
}
public void sub(int a, int b)
{
int s = a-b;
[Link]("Subtraction:"+s);
}
public void mul(int a, int b)
{
int s = a*b;
[Link]("Multiplication:"+s);
}
public void div(int a, int b)
{
int s = a/b;
[Link]("Division:"+s);
}
public void rem(int a, int b)
{
int s = a%b;
[Link]("Modules:"+s);
}
}
class Arithmetic
{
public static void main(String args[])
{
Operations O = new Operations();
Scanner s = new Scanner([Link]);
[Link]("/t/t/t/t ARITHMETIC OPERATIONS");
[Link]("/t/t/t ************ ");
[Link]("/t INPUT");
[Link]("Enter a value:");
O.a = [Link]();
[Link]("Enter b value:");
O.b = [Link]();
[Link]("/n OUTPUT");
[Link](O.a, O.b);
[Link](O.a, O.b);
[Link](O.a, O.b);
[Link](O.a, O.b);
[Link](O.a, O.b);
}
}
/t/t/t/t ARITHMETIC OPERATIONS
/t/t/t ************
/t INPUT
Enter a value:
4
Enter b value:
4
/n OUTPUT
Addition:8
Subtraction:0
Multiplication:16
Division:1
Modules:0