Java Assignment 31 To 60
Java Assignment 31 To 60
Code:
import java.util.Scanner;
class Account
{
private String customerName;
private int accountNumber;
protected double balance;
protected String accountType;
@Override
public void checkMinimumBalance(double minimumBalance, double penalty) {
if (balance < minimumBalance) { balance -= penalty;
System.out.println("Minimum balance penalty charged. Remaining balance: $" +
balance);
}
}
}
public class Lab_31 {
public static void main(String[] args) {
savingsAccount.deposit(2000);
currentAccount.withdraw(1000);
currentAccount.checkMinimumBalance(currentAccount.minimumBalance,
currentAccount.penalty);
System.out.println("\nSavings Account:");
savingsAccount.displayBalance();
scanner.close();
}
}
Output:
@Override
public void displayParentData() { super.displayParentData();
Calling parent class method using super
System.out.println("Overridden method in Child class.");
}
}
childObj.displayParentData();
childObj.displayChildData();
}
}
Output:
Que 33. Assume that the publishing company markets print books and digital
books. Create a class named Publication with data members named title, price
and authors name. from Publication class derive two classes named Books and
Ebooks. The Book class adds a page count data member named pcount while
Ebook adds data member playing time name ptime. Each of the classes must
have member functions getdata() to read class specific data from keyboard and
displaydata() to output the class specific data to the computer screen. Write a
Program to test these classes.
Code:
import java.util.Scanner;
class Publication {
String title;
double price;
String authorName;
@Override
public void getData() {
super.getData();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter page count: "); pageCount
= scanner.nextInt();
}
@Override
public void displayData()
{ super.displayData();
System.out.println("Page Count: " + pageCount);
}
}
@Override
public void getData() {
super.getData();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter playing time (in minutes): ");
playingTime = scanner.nextInt();
}
@Override
public void displayData()
{ super.displayData();
System.out.println("Playing Time: " + playingTime + " minutes");
}
}
Que 34: Assume that a shape interface contains the data members PI and
functions area () and perimeter (). Implement these two methods according to
type of shape like circle, rectangle and square classes.
Code:
interface Shape {
double PI = 3.14159;
double area();
double perimeter();
}
class Circle implements Shape {
private double radius;
public Circle(double
radius) {
this.radius = radius;
}
public double area() { return PI * radius * radius;
}
public double perimeter() {
return 2 * PI * radius;
}
}
System.out.println("Circle:");
System.out.println("Area: " + circle.area());
System.out.println("Perimeter: " + circle.perimeter());
System.out.println("\nRectangle:");
System.out.println("Area: " + rectangle.area());
System.out.println("Perimeter: " + rectangle.perimeter());
System.out.println("\nSquare:");
System.out.println("Area: " + square.area());
System.out.println("Perimeter: " + square.perimeter());
}
}
Que 35: Assume that binary interface contains the method: binary to decimal,
decimal to binary, two’s complement and binary addition. Create the
appropriate classes to implement these methods.
Code:
interface Binary {
public
String twosComplement(String binary)
{ StringBuilder complement = new StringBuilder();
for (char bit : binary.toCharArray()) {
complement.append((bit == '0') ? '1' : '0');
}
return complement.toString();
}
return sum.toString();
}
Que 36: Write a program to display the use of all access modifiers with the
help of two packages.
Code:
package package_1;
void defaultMethod() {
System.out.println("This is a default method in Package 1");
}
package package2;
import package_1.PublicClass;
}
}
Out
Que 37: Design a package to contain the class student and another package that
contains the interface sports. Write a program to display the Rollno, Paper1,
Paper2 and total score of the candidates. Code:
package studentPackage;
sportsPackage;
student.displayDetails();
Output:
System.out.println("Program completed.");
}
Output:
Que 39: Write a program to show the use of nested try/catch statements.
Code:
public class LAB_39 { public static void main(String[] args) {
System.out.println("Program completed.");
}
Output:
}
Que41: Write a program to create a custom exception. Show its use with the
help o java program. Code:
class CustomException extends Exception {
Code:
import java.util.Scanner;
class InvalidInputException extends Exception { public
InvalidInputException(String message) { super(message);
}
}
} catch (InvalidInputException e) {
if (!country.equals("India")) {
throw NoMatchException
throw new NoMatchException("String does not match 'India'");
} else {
Que44: Write a program to copy characters from one file into another using
character streams.
Code:
import java.io.*;
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
outputStream.write(bytesToWrite);
outputStream.close();
System.out.println("Bytes written to the file successfully.");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Outp
int byteRead;
while ((byteRead = inputStream.read()) != -1) {
System.out.print((char) byteRead);
}
inputStream.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
Que47: Write a program to create a sequential file that could store details of five
students. Details include id, name, class, semester, three subject marks.
Compute and print students information and their total marks. Code:
import java.io.*;
String[] students = {
"101,John,10,A,80,75,85",
"102,Alice,10,B,70,65,80",
"103,Smith,11,A,85,80,90",
"104,Emily,11,B,75,70,80",
"105,David,12,A,90,85,95"
};
fileWriter.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
bufferedReader.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
writeToFile(filePath);
readFromFile(filePath);
}
}
Que 48: Write a program to show reading and writing with random access file.
At the same time append some text to a file. Code:
import java.io.*;
writeToFile(filePath);
readFromFile(filePath);
readFromFile(filePath);
}
randomAccessFile.writeUTF("John");
randomAccessFile.writeInt(25); randomAccessFile.writeDouble(85.5);
randomAccessFile.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
randomAccessFile.seek(0);
}
}
fileWriter.write("\n" + text);
fileWriter.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Que 49: Write an applet program to print
Hello. Code: import java.applet.Applet;
import java.awt.*;
Que 50: Write an applet program to print Hello by passing parameter. Code:
import java.applet.Applet;
import java.awt.*;
String message;
Output:
addButton = new
Button("Add"); subtractButton = new
Button("Subtract"); multiplyButton = new
Button("Multiply"); divideButton = new
Button("Divide");
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
add(num1Label);
add(num1Field); add(num2Label);
add(num2Field); add(addButton);
add(subtractButton);
add(multiplyButton);
add(divideButton);
add(resultLabel);
}
Que 52: Write a program to draw various shapes (at least 5) using methods of
graphics class. Code: import java.applet.Applet; import
java.awt.*;
g.setColor(Color.red);
g.fillOval(200, 50, 100, 50);
g.setColor(Color.green);
g.drawLine(50, 150, 150, 150);
g.setColor(Color.magenta);
g.fillArc(200, 200, 100, 100, 90, 180);
}
}
Output:
Que 53: Write an applet program to draw bar charts.
Turnover (Rs.
Crores) 110 150 135 200 210 185
Code:
import java.applet.Applet;
import java.awt.*;
g.setColor(Color.black);
g.drawString(years[i], startX + (barWidth + gap) * i + 10, height - 30);
Output:
Que54: Write an applet program to insert image, audio and video
data.
Code:
package applet.programs;
import java.applet.*;
import java.awt.*;
import java.net.*;
try {
URL imageURL = new URL(getDocumentBase(), "../images/image.jpg");
image = getImage(imageURL); mediaTracker.addImage(image, 0); }
catch (MalformedURLException e) { e.printStackTrace();
}
try {
URL audioURL = new URL(getDocumentBase(), "../sound/audio.wav");
audioClip = getAudioClip(audioURL); mediaTracker.addImage(image, 1);
} catch (MalformedURLException e) { e.printStackTrace();
}
}
if
(mediaTracker.checkID(1)) { audioClip.play();
}
}
}
Que55: Write a program to illustrate the use of multithreading. Also set
priorities for threads.
Code: class MyThread extends Thread { private String threadName;
MyThread(String name)
{ threadName = name;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
}
}
hread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
}
Que56: Write a program that connects to a server by using a socket and sends
a greeting, and then waits for a response.
Code:
import java.io.*;
import java.net.*;
Que57: Write a program to create server application that uses the Socket class
to listen for clients on a port number specified by a command-line argument:
Code:
import java.io.*;
import java.net.*;
while (true) {
Que58: Write a program to show the use of methods of the ArrayList and
LinkedList classes.
Code:
import java.util.ArrayList;
import java.util.LinkedList;
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Cherry");
System.out.println("ArrayList:");
for (String fruit : arrayList) {
System.out.println(fruit);
}
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Xbox");
linkedList.add("PlayStation");
linkedList.add("Nintendo");
System.out.println("\nLinkedList:"); for
(String gameConsole : linkedList) {
System.out.println(gameConsole);
}
arrayList.add(1, "Orange");
linkedList.removeLast();
vector.add("Apple"); vector.add("Banana");
vector.add("Orange");
vector.remove("Banana");
if (vector.contains("Banana")) {
System.out.println("Vector contains Banana");
} else {
System.out.println("Vector does not contain Banana");
}
vector.clear();
if (vector.isEmpty()) {
System.out.println("Vector is empty");
} else {
System.out.println("Vector is not empty");
}
}
Code:
import java.util.ArrayList;
import java.util.Collections;
if (index >= 0) {
System.out.println("Element " + target + " found at index " + index);
} else {
System.out.println("Element " + target + " not found");
}
}
}