Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

INDEX

S NO. TITLE P.NO T.SIGN


1 Write a program to print a Hello World
in Java.

2 write a program to execute two


numbers of sum in Java.

3 write a program to execute an if


statement in Java.

4 Check whether a number is even or


odd using the if...else statement.

5 Write a program to execute an if-


nested statement.

6 Write a program to calculate the grade


of students.
7 Java program to calculate the week’s
name
8 Write a program to print numbers from
1 to 10 form using for loop.
9 write a program to create a half-
pyramid pattern using nested loops

10 Write a program to execute ALL


Methods of string and all methods of
buffer string.
11 Write a program to calculate to Add
Elements to Vector.

12 Write a program to declare the two


Box objects.

13 which has two methods that add


numbers of different types using
function overloading.

1
21 Write a program to execute the
random file accesses.

22 write a program, to execute a


multithreading.

14. write a program, to execute an


inheritance and types of inheritances.

15 write a program, to execute a package


and types of packages.

16 write a program, to execute an Array


and Interface.
17 how a run-time system searches for
appropriate exception handling code
on the call stack
18 write a program to create a file by
using iostream.
19 Write a program to create the file and
read and write the file.

20 write a Java program to merge the


content of two files into a third file.
The user must receive the name of all
three files at the run-time of the
program.

2
1. Write a program to print a Hello World in Java.
Code:
public class Main {
public static void main (String[] args){
System.out.println("Hello World");
}
}

OUTPUT

3
2. write a program to execute two numbers of sum in Java.
CODE:
public class Main {
public static void main(String[] args){
int x = 10;
int y = 20;
int s = x + y;
System.out.println(“enter the First Number = "+ x);
System.out.println(“enter the Second Number =” + y);
System.out.println("The sum of two Number =" + s);
}
}
OUTPUT

4
3. write a program to execute an if statement in Java.
CODE:
public class X {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
OUTPUT

5
4. Check whether a number is even or odd using the if...else
statement.
CODE:

public class ABC {


public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
}
else {
System.out.println("Good evening.");
}
}
}

OUTPUT

6
5. Write a program to execute an if-nested statement.
CODE:
public class Test {

public static void main(String args[]) {


int x = 30;
int y = 10;

if( x == 30 ) {
if( y == 10 ) {
System.out.print("X = 30 and Y = 10");
}
}
}
}

OUTPUT

7
6. Write a program to calculate the grade of students.
CODE:

import java.util.Scanner;
public class Grade
{
public static void main(String[] args)
{
int score;
char grade;
Scanner console = new Scanner(System.in);
System.out.print("Enter your numeric test score : ");
score = console.nextInt();

if (score >= 90)


{
grade = 'A';
}
else if (score >= 80)
{
grade = 'B';
}
else if (score >= 70)
{
grade = 'C';
}

else if (score >= 50)


{
grade = 'D';
}
else
{

grade = 'F';
8
}
System.out.println("Your grade is " + grade);
}
}
OUTPUT

7.Java program to calculate the week’s name

9
CODE:

public class Main {

public static void main(String[] args) {

int day = 4;

switch (day) {

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

10
break;

case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");

break;

OUTPUT

11
8. Write a program to print numbers from 1 to 10 form using for
loop.
CODE:
class PrintNumbers
{
public static void main(String[] args)
{
for(int i=1; i<=10; i++)
{
System.out.println(i);
}
}
}
OUTPUT

II)This program will try to print “Hello World” 5 times. For using
a while loop.
CODE:
class whileLoopDemo {
public static void main(String args[])

12
{

int i = 1;

while (i < 6) {
System.out.println("Hello World");

i++;
}
}
}

OUTPUT

III) . Write a program to print numbers from 1 to 10 form using the


Do loop.
CODE:
public class DoWhileExample {
public static void main(String[] args) {

int i=1;

do{

System.out.println(i);
i++;
}

while(i<=10);

13
}
}

OUTPUT

14
9. write a program to create a half-pyramid pattern using nested
loops

CODE:
class Main {
public static void main(String[] args) {

int rows = 5;

// outer loop
for (int i = 1; i <= rows; ++i) {

// inner loop to print the numbers


for (int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println("");
}
}
}

OUTPUT

15
10. Write a program to excute ALL Methods of string and all
methods of buffer string.

length(): This method is used to find the length of a string.


CODE:

public class string {


public static void main(String[] args) {
String quote = "To be or not to be";
System.out.println(quote.length());
}
}
OUTPUT

indexOf(): This method returns the first occurrence of a specified


character or text in a string.

CODE:
public class string {
public static void main(String[] args) {
String quote = "To be or not to be";

16
System.out.println(quote.indexOf("be")); //index of text
System.out.println(quote.indexOf("r")); //index of character
}
}

OUTPUT

toLowerCase() and toUpperCase(): Converts string to lower case


AND upper class characters and Converts string to upperr case
AND lower class characters

CODE:
public class string {
public static void main(String[] args) {
String quote = "THOR: Love and Thunder";
System.out.println(quote.toLowerCase());

17
System.out.println(quote.toUpperCase());
}
}
OUTPUT

1. StringBuffer Class append() Method and insert()


method

class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");
System.out.println(sb);
sb.insert(1,"Java");
System.out.println(sb);
}
}

OUTPUT

18
2.StringBuffer replace() Method AND delete() Method

CODE
class StringBufferExample3{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb);
}
}
OUTPUT

3. StringBuffer reverse() Method AND apacity() Method


AND ensureCapacity() method.
CODE:
class StringBufferExample5{
public static void main(String args[]){

19
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
OUTPUT

20
CODE:
class StringBufferExample7{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34
i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}

OUTPUT

21
11. Write a program to calculate to Add Elements to Vector.
CODE:
import java.util.Vector;

class Main {
public static void main(String[] args) {
Vector<String> mammals= new Vector<>();

mammals.add("Dog");
mammals.add("Horse");

mammals.add(2, "Cat");
System.out.println("Vector: " + mammals);

Vector<String> animals = new Vector<>();


animals.add("Crocodile");

animals.addAll(mammals);
System.out.println("New Vector: " + animals);
}
}

22
OUTPUT

12. Write a program to declare the two Box objects.

CODE:

class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[])
{
Box mybox1 = new Box(Box mybox2 = new Box();
double vol;
variables mybox1.width = 10;
mybox1.height =20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
vol=mybox1.width*mybox1.height*mybox1.depth;
System.out.println("Volume is " + vol);

23
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}

OUTPUT

24
13. which has two methods that add numbers of different types
using function overloading.
CODE:
public class Main {
static int plusMethodInt(int x, int y) {
return x + y;
}

static double plusMethodDouble(double x, double y) {


return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
}
OUTPUT

25
14 write a program, to execute an inheritance and types of
inheritances.

SIMPLE INHERITANCES
CODE:
class FundamentalForce {
void Force() {
System.out.println("There are four fundamental forces.");
}
}
class Gravitational extends FundamentalForce {
void Gravity() {
System.out.println("Fruits fall to the ground due to
gravitational Force.");
}
}
class SingleInheritance {
public static void main(String[] args) {
Gravitational G = new Gravitational();
G.Force();
G.Gravity();

26
}
}
OUTPUT

Multi-level Inheritance
CODE:
class NuclearForce extends FundamentalForce {
void Nuclear() {
System.out.println("Nuclear Forces are of two types;");
System.out.println("Strong Nuclear Force");
System.out.println("Weak Nuclear Force");
}
}

class StrongNuclearForce extends NuclearForce {


void Strong() {
System.out.println("Strong Nuclear Force is responsible for
the underlying stability of matter.");
}
}

class MultilevelInheritance {
public static void main(String[] args) {

27
StrongNuclearForce st = new StrongNuclearForce();
st.Force();
st.Nuclear();

st.Strong();
}
}
OUTPUT

Hierarchical Inheritance
CODE:
class FundamentalForce {
void Force() {
System.out.println("There are four fundamental forces.");
}
}

28
class Gravitational extends FundamentalForce {
void Gravity() {
System.out.println("Fruits fall to the ground due to
gravitational Force.");
}
}

class Electromagnetic extends FundamentalForce {


void Particles() {
System.out.println("The electromagnetic force acts between
charged particles");
}
}
class HierarchicalInheritance {
public static void main(String[] args) {
System.out.println("Child 1:");
Gravitational G = new Gravitational();
G.Force();
G.Gravity();
System.out.println();
System.out.println("Child 2");
Electromagnetic em = new Electromagnetic();
em.Force();
em.Particles();
}
}

29
OUTPUT

15 write a program to excute a package.


i) Pre-defined packages
CODE:

import static java.lang.System.*;

class StaticImportDemo
{
public static void main(String args[])
{

System.out.println("This is java Program.");


}
}

OUTPUT

USER-DEFINED PACKAGES
30
CODE:
package tulsi;

public class gupta {


public static void msg() {
System.out.println("1990");
}
}

public class javaException{


public static void main(String args[]){
try{

int data=100/0;
}
catch(ArithmeticException e){System.out.println(e);}

System.out.println("rest of the code...");


}
}

OUTPUT

31
16. i) Write a Java program to sum the values of an array.

CODE:

public class Exercise2 {


public static void main(String[] args) {
int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;

for (int i : my_array)


sum += i;
System.out.println("The sum is " + sum);
}
}

OUTPUT

32
ii) write a program to execute the student of details using the
interface.
CODE:
interface ShowDetails
{

void showDetail(String name, int age);


}

public class InterfaceExample1 implements ShowDetails


{

@Override

public void showDetail(String name, int age)


{
System.out.println("Name = " + name);
System.out.println("Age = " + age);
}

public static void main(String args[])


{

InterfaceExample1 obj = new InterfaceExample1();


obj.showDetail("jai", 26);
}
}

OUTPUT

33
17. how a run-time system searches for appropriate exception
handling code on the call stack

CODE:
class GFG {

static int divideByZero(int a, int b)


{

int i = a / b;

return i;
}

static int computeDivision(int a, int b)


{

int res = 0;

try {

res = divideByZero(a, b);


}

34
catch (NumberFormatException ex) {

System.out.println(
"NumberFormatException is occurred");
}
return res;
}

public static void main(String args[])


{

int a = 1;
int b = 0;

try {
int i = computeDivision(a, b);
}

catch (ArithmeticException ex) {

System.out.println(ex.getMessage());
}
}
}

OUTPUT

35
18. write a program to create a file by using iostream.
CODE:
import java.io.File;
import java.io.IOException;
public class CreateFile {
public static void main(String[] args) {
try {
File myObj = new File("students.txt");
if (myObj.createNewFile()) {
System.out.println("Created Successfully: " +
myObj.getName());
} else {
System.out.println("Sorry, File Exists.");
}
} catch (IOException e) {
36
System.out.println("Error.....");
e.printStackTrace();
}
}
}
OUTPUT

19. Write a program to create the file and read and write the file.
CODE:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {


public static void main(String[] args) {
try {
File file = new File("students.txt");
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader

37
.close();
} catch (FileNotFoundException e) {
System.out.println("Error.....");
e.printStackTrace();
}
}
}

OUTPUT

WRITE THE FILE


CODE:
import java.io.FileWriter;
import java.io.IOException;

public class WriteFile {


public static void main(String[] args) {
try {
FileWriter file = new FileWriter("students.txt");

38
file.write("This file has information of Teachers");
file.write("Name: Rosy Fernandes, Age: 36");
file.write("Name: Isha Mheta, Age: 29");
file.close();
System.out.println("File has been written into.");
} catch (IOException e) {
System.out.println("Error......");

e.printStackTrace();
}
}
}

OUTPUT

39
20. write a Java program to merge the content of two files into a
third file. The user must receive the name of all three files at the
run-time of the program.
CODE:
import java.io.*;

public class FileMerge


{
public static void main(String[] args) throws IOException
{
// PrintWriter object for file3.txt
PrintWriter pw = new PrintWriter("FileMergejava");

// BufferedReader object for file1.txt


BufferedReader br1 = new BufferedReader(new
FileReader("A.txt"));
BufferedReader br2 = new BufferedReader(new
FileReader("B.txt"));

40
String line1 = br1.readLine();
String line2 = br2.readLine();
while (line1 != null || line2 !=null)
{
if(line1 != null)
{
pw.println(line1);

line1 = br1.readLine();
}

if(line2 != null)
{
pw.println(line2);
line2 = br2.readLine();
}
}

pw.flush();
br1.close();
br2.close();
pw.close();
}
}
FILE A.TXT

41
B.TXT

OUTPUT

C.TXT

42
21. Write a program to execute the random file accesses.
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileExample {


static final String FILEPATH ="A.txt";
public static void main(String[] args) {
try {
System.out.println(new String(readFromFile(FILEPATH, 0,
18)));
writeToFile(FILEPATH, "I love my country and my people",
31);
} catch (IOException e) {
e.printStackTrace();
}

43
}
private static byte[] readFromFile(String filePath, int position, int
size)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath,
"r");
file.seek(position);
byte[] bytes = new byte[size];

file.read(bytes);
file.close();
return bytes;
}

private static void writeToFile(String filePath, String data, int


position)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath,
"rw");
file.seek(position);
file.write(data.getBytes());
file.close();
}
}

44
OUTPUT

22. write a program to execute the multithreading .java

CODE:

public class e extends Thread


{
public void run()
{
System.out.println("Thread is running...");
}
public static void main(String args[])
{
e t1=new e();

t1.start();
}
}

OUTPUT

45
46

You might also like