Java Assignment Editing
Java Assignment Editing
Code:
Import java.lang.*;
import java.util.Scanner;
class temperature{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int j=0 ; j<5 ;j++) {
System.out.print("Enter temperature in fahrenheit: "+" ");
Double fahrenheit = sc.nextDouble();
Double celsius = (fahrenheit - 32) / 1.8;
System.out.println("celcious temprature is "+ celsius);
}
sc.close();
}
}
Output:
Page-1
Assignment A/02:
Write a program to find the number of and sum of all integers greater than 100
and less than 200 that are divisible by 7.
Code:
Import java.lang.*;
class DivisibleBySeven {
public static void main(String[] args) {
int count = 0;
int sum = 0;
Output:
Page-2
Assignment A/03:
Write a program to print Floyd’s triangle.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Code:
import java.lang.*;
import java.util.Scanner;
class FloydTriangle{
public static void main (String[]args){
int i,j;
Page-3
Output:
Page-4
Assignment A/04:
Write a program to reverse the digits of the number.
Code:
import java.lang.*;
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
int reversedNumber = 0;
while (number != 0) {
int remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number = number / 10;
}
System.out.println("Reversed number: " +
reversedNumber);
sc.close();
}
}
Output:
Page-5
Assignment A/05:
Write a program to evaluate the following investment equation.
V = P * (1 + R) N
and print the tables which would give the value of V for various combinations
of the following values of P, R and N.
Code:
import java.lang.*;
import java.util.Scanner;
System.out.println("\nYear\tValue");
System.out.println("-------------");
sc.close();
}
}
Page-6
Output:
Page-7
Assignment B/01:
Given are two 1D array A and B which are sorted in ascending order. Write a
program to merge them into a single sorted array C that contains every item
from array A and B in ascending order.
Code:
import java.lang.*;
import java.util.Arrays;
public class MergeSortedArrays {
public static void main(String[] args) {
int[] A = {1, 3, 5, 7};
int[] B = {2, 4, 6, 8};
int[] C = new int[A.length + B.length];
int i = 0, j = 0, k = 0;
System.out.print("Array A :");
System.out.println(Arrays.toString(A));
System.out.print("Array B :");
System.out.println(Arrays.toString(B));
Page-8
Output:
Page-9
Assignment B/02:
Write a program that will read the values of elements of A and B and produce
the product matrix C.
Code:
import java.lang.*;
import java.util.Scanner;
class MatrixProduct {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create matrix A
int[][] matrixA = new int[rowsA][colsA];
System.out.println("Enter the elements of matrix A:");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
matrixA[i][j] = scanner.nextInt();
}
}
// Create matrix B
int[][] matrixB = new int[rowsB][colsB];
System.out.println("Enter the elements of matrix B:");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
matrixB[i][j] = scanner.nextInt();
}
}
Page-10
// Create matrix C (product matrix)
int[][] matrixC = new int[rowsA][colsB];
}
}
Output:
Page-11
Assignment B/03:
Write a program, which will read a text and count all occurrences of a
particular word.
Code:
import java.lang.*;
import java.util.Scanner;
public class WordCounter {
public static void main(String[] args) {
// Read the text from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the text: ");
String text = scanner.nextLine();
return count;
}
} Page-12
Output:
Page-13
Assignment B/04:
Write a program which will read a string and rewrite it in the alphabetical
order. For example, the word STRING should be written as GINRST.
Code:
import java.lang.*;
import java.util.Arrays;
import java.util.Scanner;
Output:
Page-14
Assignment B/05:
Write a program that will count the number of characters in a file.
Code:
import java.lang.*;
import java.io.FileReader;
import java.io.IOException;
Page-15
Assignment C/01:
Write a program to create a distance class with methods where distance is
computed in terms of feet and inches, how to create objects of a class and to
see the use of this pointer.
Code:
import java.lang.*;
import java.util.Scanner;
class Distance {
private int feet;
private int inches;
Page-16
System.out.println("Enter the second distance:");
System.out.print("Feet: ");
int feet2 = scanner.nextInt();
System.out.print("Inches: ");
int inches2 = scanner.nextInt();
Distance distance2 = new Distance(feet2, inches2);
System.out.println("Distances entered:");
distance1.display();
distance2.display();
distance1.add(distance2);
System.out.println("After adding distances:");
distance1.display();
scanner.close();
}
}
Output:
Page-17
Assignment C/02:
Modify the distance class by creating constructor for assigning values (feet and
inches) to the distance object. Create another object and assign the second
object as reference variable to another object reference variable. Further
create a third object which is a clone of the first object.
Code:
import java.lang.*;
import java.util.Scanner;
class Distance {
private int feet;
private int inches;
@Override
public String toString() {
return feet + " feet, " + inches + " inches";
}
scanner.close();
}
}
Output:
Page-19
Assignment C/03:
Write a program to show that during function overloading, if no matching
argument is found, then java will apply automatic type conversions (from
lower to higher data type).
Code:
import java.lang.*;
public class TypeConversion {
// Method to add two integers
public static int add(int a, int b) {
System.out.println("Adding two integers");
return a + b;
}
Page-20
double mixedResult = add(7, 4.5);
System.out.println("Result of adding mixed types: " +
mixedResult);
}
}
Output:
Page-21
Assignment C/04:
Write a program to show the use of static functions.
Code:
import java.lang.*;
import java.util.Scanner;
Output:
Page-23
Assignment C/05:
Write a program to implementation of pass variable length arguments in a
function.
Code:
import java.lang.*;
public class VarArgs {
System.out.println();
}
Output:
Page-24
Assignment D/01:
Write a program to illustrate multilevel inheritance.
Code:
import java.lang.*;
class Animal {
void eat() {
System.out.println("The animal is eating.");
}
}
class Mammal extends Animal {
void run() {
System.out.println("The mammal is running.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog is barking.");
}
}
public class MultilevelInheritance {
public static void main(String[] args) {
Dog myDog = new Dog();
Output:
Page-25
Assignment D/02:
Write a program to illustrate multiple inheritances using interface.
Code:
import java.lang.*;
// Define two interfaces
interface A {
void methodA();
}
interface B {
void methodB();
}
// Implement the interfaces in a class
class MyClass implements A, B {
@Override
public void methodA() {
System.out.println("Method A implementation");
}
@Override
public void methodB() {
System.out.println("Method B implementation");
}
}
public class Interfaces {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.methodA();
obj.methodB();
}
}
Output:
Page-26
Assignment D/03:
Write a program to show the calling sequence of construction of super and sub
class.
Code:
import java.lang.*;
class Superclass {
public Superclass() {
System.out.println("Superclass constructor");
}
Output:
Page-27
Assignment D/04:
Write a program to show function overriding and dynamic method dispatch.
Code:
import java.lang.*;
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
Output:
Page-28
Assignment D/05:
Write a program that creates illustrates different levels of protection in
classes/subclasses belonging to same packages.
Code:
import java.lang.*;
package mypackage;
class Parent {
private int privateVar = 10;
int defaultVar = 20;
protected int protectedVar = 30;
public int publicVar = 40;
void display() {
System.out.println("Private Variable: " + privateVar);
System.out.println("Default Variable: " + defaultVar);
System.out.println("Protected Variable: " + protectedVar);
System.out.println("Public Variable: " + publicVar);
}
}
System.out.println("----------------");
Output:
Page-30
Assignment E/01:
Write a program to illustrate index out of bound exception.
Code:
import java.lang.*;
public class IndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
int[] numbers = { 1, 2, 3, 4, 5 };
try {
int index = 10; // Trying to access an index that is out
of bounds
int value = numbers[index];
System.out.println("Value at index " + index + ": " +
value);
} catch (IndexOutOfBoundsException e) {
System.out.println("IndexOutOfBoundsException caught: "
+ e.getMessage());
}
}
}
Output:
Page-31
Assignment E/02:
Write a program to create your own exception types to handle situation
specific to your application.
Code:
import java.lang.*;
import java.util.Scanner;
// Custom exception class
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
// Application class
class MyApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
if (age < 0) {
throw new CustomException("Age cannot be negative");
}
System.out.println("Age: " + age);
} catch (CustomException e) {
System.out.println("Error: " + e.getMessage());
} finally {
scanner.close();
}
}
}
Output:
Page-32
Assignment E/03:
Write a program to demonstrate priorities among multiple threads.
Code:
import java.lang.*;
class Priority implements Runnable {
private String name;
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(name + ": " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
thread1.start();
thread2.start();
}
}
Page-33
Output:
Page-34
Assignment F/01:
Write a program that create a Banner and then creates a thread to scroll the
message in the banner from left to right across the applet's window.
Code:
File-1 :
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
@Override
public void init() {
// Set the size of the applet window
setSize(400, 100);
// Create a thread to scroll the message
Thread scrollThread = new Thread(this);
scrollThread.start();
}
Page-35
@Override
public void paint(Graphics g) {
super.paint(g);
// Clear the applet's window
g.clearRect(0, 0, getWidth(), getHeight());
// Check if the message has moved off the right side of the
window
if (xCoordinate >= getWidth()) {
// Reset the x coordinate to the left side of the window
xCoordinate = -g.getFontMetrics().stringWidth(message);
}
}
@Override
public void stop() {
// Stop the thread when the applet is stopped or destroyed
running = false;
}
}
File-2 :
<html>
<head>
<title>My First Applet Program</title>
</head>
<body>
<applet code =BannerApplet.class width = 600 height =
100></applet>
</body>
</html>
Note:
File-2 save as a MyApplet.html.
Page-36
Output:
Page-37
Assignment F/04:
Write a program to generate a windows without an applet window.
Code:
import java.lang.*;
import javax.swing.*;
Page-38
Output:
Page-39
Assignment F/05:
Write a program to demonstrate the use of push buttons.
Code:
File-1 :
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
Button clickButton;
Label clickLabel;
int clickCount;
clickButton.addActionListener(this);
add(clickButton);
add(clickLabel);
}
Page-40
File-2 :
<html>
<head>
<title>My First Applet Program</title>
</head>
<body>
<applet code =PushButtonDemo.class width = 600 height =
100></applet>
</body>
</html>
Note:
File-2 save as a MyApplet.html.
Output:
Page-41
INDEX
S ASSIGNMENT DATE OF PAGE SIGNATURE
NO. NO. ASSIGNMENT NO.
1 A/01, A/02, A/03 02/03/23 1-4