0% found this document useful (0 votes)
35 views12 pages

Week - 7: Problems Based On If statement/Looping/Array/Strings in Java

The document discusses Java programs involving classes, objects, constructors, arrays, strings, and loops. It provides the source code and output for 5 problems involving binary search, sorting an array, storing and displaying a string, reversing a string, and checking if a string is a palindrome. It also provides the source code and output for 4 problems involving creating classes to represent fruits with attributes like color, taste, and price and using methods and constructors to set and display object attributes.

Uploaded by

Ranjeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

Week - 7: Problems Based On If statement/Looping/Array/Strings in Java

The document discusses Java programs involving classes, objects, constructors, arrays, strings, and loops. It provides the source code and output for 5 problems involving binary search, sorting an array, storing and displaying a string, reversing a string, and checking if a string is a palindrome. It also provides the source code and output for 4 problems involving creating classes to represent fruits with attributes like color, taste, and price and using methods and constructors to set and display object attributes.

Uploaded by

Ranjeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Week - 7

Problems Based on If statement/Looping/Array/Strings in Java

1# Write a java program to implement binary search.


Source Code:
public class Binary_Search {

public static void main(String[] args) {

System.out.println("A program to implement binary search:\n");

int[] ar = {11, 55, 77, 222, 888};

int search = 888;


int first_ar = 0;
int last_ar = ar.length - 1;

int mid = (first_ar + last_ar)/2;

while (first_ar <= last_ar)


{
if (ar[mid] < search)
first_ar = mid + 1;

else if (ar[mid] == search)


{
System.out.print(search + " is found in the given array.");
break;
}

else
last_ar = mid - 1;

mid = (first_ar + last_ar)/2;


}

if (first_ar > last_ar)


System.out.print(search + " is not found in the given array.");
}
}

Result:
2# Write a java program to arrange the elements of an array in ascending order(Sorting).
Source Code:
public class Sorting {

public static void main(String[] args) {

System.out.println("A program to arrange the elements of an array in an


ascending order:");

int[] ar = {11, 585, 177, 222, 9};


int len = ar.length;

System.out.print("\nBefore sorting the array is: {11, 585, 177, 222, 9}");

int temp;
for (int i = 0; i < len - 1; i++)
for (int j = 0; j < len - 1 - i; j++)
if (ar[j] > ar[j + 1])
{
temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}

System.out.print("\nAfter sorting the array is: {");


for (int i = 0; i < len; i++)
if (i != len - 1)
System.out.print(ar[i] + ", ");
else
System.out.print(ar[i]);
System.out.print("}");
}
}

Result:

3# Write a java program to store 'Java is awesome' in a string and display it.
Source Code:
public class String_Print {

public static void main(String[] args) {

System.out.println("A program to store \"Java is awesome\" in a string and


display it :\n");

String message = "Java is awesome";


System.out.print(message);
}
}
Result:

4# Write a java program to reverse a given string.


Source Code:
import java.util.Scanner;

public class Reverse {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);

System.out.println("A program to reverse a string :");


System.out.print("\nEnter string = ");
String str = in.nextLine();
String rev_str = "";

for (int i = str.length() - 1; i >= 0 ; i--)


rev_str += str.charAt(i);

System.out.printf("\nEntered string : %s\nReversed string : %s", str, rev_str);


}
}

Result:

5# Write a java program to check whether a given string is palindrome or not.

Source Code:
import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);

System.out.println("A program to check whether given string is palindrome or


not:");
System.out.print("\nEnter string = ");
String str = in.nextLine();

int check = 0;
for (int i = 0; i < str.length()/2 ; i++)
{
if (str.charAt(i) != str.charAt(str.length() - 1- i))
{
System.out.println("\n\"" + str + "\" is not a palindrome.");
check = 1;
break;
}
}
if (check == 0)
System.out.println("\n\"" + str + "\" is a palindrome.");
}
}

Result:

Problems Based on HTML:


1# Design an HTML Page having 3 images placed in the following format.

Source Code:
<html>
<head>
<title>Week 7(1) Image Table</title>
</head>
<body>
<table>
<tr>
<td><img src="Window.jpg"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><img src="Window.jpg"></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td><img src="Window.jpg"></td>
</tr>
</table>
</body>
</html>

Result:

2# Write HTML code to generate the following output:

Source Code:
<html>
<head>
<title>Week 7(2) Table</title>
</head>
<body>
<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td rowspan="2" colspan="2"><img src="Window.jpg"></td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</table>
</body>
</html>

Result:
Week - 8
Problems Based on Object/Class/Constructor in Java
1# Create a class FRUIT which has data members color, taste and price. Also create a
method display() which will print values of FRUIT object. Create three objects of FRUIT
class and call their display() methods.

Souce Code:
public class FRUIT {

String colour;
String taste;
double price;

void display()
{
System.out.println("Colour: " + colour);
System.out.println("Taste: " + taste);
System.out.println("Price: " + price);
}

public static void main(String[] args) {

FRUIT apple = new FRUIT();


FRUIT orange = new FRUIT();
FRUIT banana = new FRUIT();

apple.colour = "Red";
apple.taste = "Sweet";
apple.price = 60;

orange.colour = "Orange";
orange.taste = "Sweet & Sour";
orange.price = 35;

banana.colour = "Yellow";
banana.taste = "Sweet";
banana.price = 25;

System.out.println("Apple:-");
apple.display();
System.out.println("\nOrange:-");
orange.display();
System.out.println("\nBanana:-");
banana.display();
}
}

Result:
2# Create a class FRUIT which has data members color, taste and price. It has a method
setDetails() which will set the values of color, taste and price. Also create a method
display() which will print values of FRUIT object.

Source Code:
public class FRUIT {

String colour;
String taste;
double price;

void setDetails(String colour_, String taste_, double price_)


{
colour = colour_;
taste = taste_;
price = price_;
}

void display()
{
System.out.println("Colour: " + colour);
System.out.println("Taste: " + taste);
System.out.println("Price: " + price);
}

public static void main(String[] args) {

FRUIT apple = new FRUIT();


FRUIT orange = new FRUIT();
FRUIT banana = new FRUIT();

apple.setDetails("Red", "Sweet", 50);


orange.setDetails("Orange", "Sweet & Sour", 35);
banana.setDetails("Yellow", "Sweet", 30);

System.out.println("Apple:-");
apple.display();
System.out.println("\nOrange:-");
orange.display();
System.out.println("\nBanana:-");
banana.display();
}
}

Result:

3# In previous question, set the values of using color, taste and price using Constructor.

Source Code:
public class FRUIT {
String colour;
String taste;
double price;

FRUIT(String colour_, String taste_, double price_)


{
colour = colour_;
taste = taste_;
price = price_;
}

void display()
{
System.out.println("Colour: " + colour);
System.out.println("Taste: " + taste);
System.out.println("Price: " + price);
}

public static void main(String[] args) {

FRUIT apple = new FRUIT("Red", "Sweet", 90);


FRUIT orange = new FRUIT("Orange", "Sweet & Sour", 35);
FRUIT banana = new FRUIT("Yellow", "Sweet", 30);

System.out.println("Apple:-");
apple.display();
System.out.println("\nOrange:-");
orange.display();
System.out.println("\nBanana:-");
banana.display();
}
}

Result:

4# Add one-argument constructor and two-argument constructor in addtion to default


constructor in FRUIT class.

Source Code:
public class FRUIT {

String colour = "Red";


String taste = "Sweet";
double price = 30;

FRUIT(double price_)
{
price = price_;
}

FRUIT(String colour_, double price_)


{
colour = colour_;
price = price_;
}

FRUIT(String colour_, String taste_, double price_)


{
colour = colour_;
taste = taste_;
price = price_;
}

void display()
{
System.out.println("Colour: " + colour);
System.out.println("Taste: " + taste);
System.out.println("Price: " + price);
}

public static void main(String[] args) {

FRUIT mango = new FRUIT("Yellow", 25);


FRUIT orange = new FRUIT("Orange", "Sweet & Sour", 35);
FRUIT pomegranate = new FRUIT(60);

System.out.println("Mango:-");
mango.display();
System.out.println("\nOrange:-");
orange.display();
System.out.println("\nPomegranate:-");
pomegranate.display();
}
}

Result:

5# Use the concept of constructor-chaining in the previous question using this().

Source Code:
public class FRUIT {

String colour = "Red";


String taste = "Sweet";
double price = 40;

FRUIT(double price_)
{
price = price_;
}

FRUIT(String colour_, double price_)


{
this(price_);
colour = colour_;
}
FRUIT(String colour_, String taste_, double price_)
{
this(colour_, price_);
taste = taste_;
}

void display()
{
System.out.println("Colour: " + this.colour);
System.out.println("Taste: " + this.taste);
System.out.println("Price: " + this.price);
}

public static void main(String[] args) {

FRUIT mango = new FRUIT("Yellow", 25);


FRUIT orange = new FRUIT("Orange", "Sweet & Sour", 35);
FRUIT pomegranate = new FRUIT(60);

System.out.println("Mango:-");
mango.display();
System.out.println("\nOrange:-");
orange.display();
System.out.println("\nPomegranate:-");
pomegranate.display();
}
}
Result:
Problems Based on HTML:

1# Write an HTML code to develop a Web page having two frames that divide the Web
page into two equal rows and then divide the second row into two equal columns, then fill
each frame with a different background colour.

Source Code:
<!DOCTYPE html>
<html>
<head>
<title>Week 8 Frames</title>
</head>
<frameset rows="50%, 50%">
<frame src = "Frame1.html">
<frameset cols="50%, 50%">
<frame src="Frame2(a).html">
<frame src="Frame2(b).html">
</frameset>
</frameset>
</html>

Frame1.html Frame2(a).html Frame2(b).html

<!DOCTYPE html> <!DOCTYPE html> <!DOCTYPE html>


<html> <html> <html>
<head> <head> <head>
<title>Frame 1</title> <title>Frame 2(a)</title> <title>Frame 2(b)</title>
</head> </head> </head>
<body bgcolor="red"> <body bgcolor="blue"> <body bgcolor="green">
<h1>Frame 1</h1> <h1>Frame 2(a)</h1> <h1>Frame 2(b)</h1>
</body> </body> </body>
</html> </html> </html>

Result:

You might also like