java 1 to 43 questions
java 1 to 43 questions
class Cat {
String name;
int age;
Cat() {
name = "Unknown";
age = 0;
Output:
class Dog {
this.name = name;
this.colour = colour;
}
}
Output:
class Book {
double price;
Book() {}
this.title = title;
this.author = author;
this(title, author);
this.price = price;
Output:
Book1, Author1
int studentId;
String studentName;
char grade;
Student() {}
this.studentId = studentId;
this.studentName = studentName;
this.grade = grade;
Output:
101, John, A
import java.util.ArrayList;
class Book {
this.title = title;
this.author = author;
this.ISBN = ISBN;
}
static void addBook(Book book) {
collection.add(book);
collection.remove(book);
addBook(b1);
removeBook(b1);
Output:
Collection: 1
Collection: 0
class Account {
String accountNumber;
double balance;
} else {
this.accountNumber = accountNumber;
this.balance = balance;
Output:
java
Copy code
class Car {
int year;
}
}
Output:
sql
Copy code
java
Copy code
class Point {
double x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
Point(double x, double y) {
this.x = x;
this.y = y;
Output:
makefile
Copy code
java
Copy code
class Constants {
Output:
makefile
Copy code
Area: 78.54
java
Copy code
class Animal {
void makeSound() {
System.out.println("Some sound");
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow");
animal.makeSound();
Output:
Copy code
Meow
java
Copy code
class Vehicle {
void drive() {
System.out.println("Driving a vehicle");
@Override
void drive() {
System.out.println("Repairing a car");
v.drive();
Output:
css
Copy code
Repairing a car
java
Copy code
class Shape {
double getArea() {
return 0;
this.length = length;
this.width = width;
@Override
double getArea() {
}
public static void main(String[] args) {
Output:
makefile
Copy code
Area: 15.0
java
Copy code
class Employee {
void work() {
System.out.println("Working...");
double getSalary() {
return 50000;
@Override
void work() {
System.out.println("Managing HR tasks");
void addEmployee() {
System.out.println("Employee added");
}
hr.work();
hr.addEmployee();
Output:
Copy code
Managing HR tasks
Employee added
java
Copy code
class BankAccount {
double balance;
balance += amount;
balance -= amount;
@Override
} else {
super.withdraw(amount);
System.out.println("Withdrawal successful");
acc.deposit(200);
acc.withdraw(150);
acc.withdraw(50);
Output:
yaml
Copy code
Withdrawal successful
java
Copy code
class Animal {
void move() {
System.out.println("Animal is moving");
void move() {
a.move();
Output:
arduino
Copy code
java
Copy code
class Person {
String getFirstName() {
return "John";
String getLastName() {
return "Doe";
int getEmployeeId() {
return 123;
}
@Override
String getLastName() {
Output:
java
Copy code
java
Copy code
class Shape {
double getPerimeter() {
return 0;
double getArea() {
return 0;
}
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
@Override
double getPerimeter() {
@Override
double getArea() {
Output:
mathematica
Copy code
java
Copy code
class Vehicle {
String make, model;
int year;
double fuelEfficiency() {
return 0;
@Override
double fuelEfficiency() {
@Override
double fuelEfficiency() {
return 8;
Output:
yaml
Copy code
Car fuel efficiency: 15.0
java
Copy code
class Employee {
double salary;
double calculateBonus() {
@Override
double calculateBonus() {
m.salary = 100000;
Output:
yaml
Copy code
@Override
void sound() {
System.out.println("Roar");
@Override
void sound() {
System.out.println("Growl");
lion.sound();
tiger.sound();
Output:
Roar
Growl
4o
21. Abstract class Shape with subclasses Circle and Triangle
java
Copy code
double radius;
Circle(double radius) {
this.radius = radius;
@Override
double calculateArea() {
@Override
double calculatePerimeter() {
Triangle(double base, double height, double sideA, double sideB, double sideC) {
this.base = base;
this.height = height;
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
@Override
double calculateArea() {
@Override
double calculatePerimeter() {
Output:
mathematica
Copy code
java
Copy code
@Override
void eat() {
@Override
void sleep() {
@Override
void eat() {
@Override
void sleep() {
@Override
void eat() {
@Override
void sleep() {
lion.eat();
lion.sleep();
tiger.eat();
tiger.sleep();
deer.eat();
deer.sleep();
Output:
Copy code
java
Copy code
@Override
void startEngine() {
@Override
void stopEngine() {
@Override
void startEngine() {
@Override
void stopEngine() {
car.startEngine();
car.stopEngine();
bike.startEngine();
bike.stopEngine();
Output:
Copy code
java
Copy code
interface Shape {
double getArea();
this.width = width;
@Override
double radius;
Circle(double radius) {
this.radius = radius;
@Override
this.base = base;
this.height = height;
}
@Override
Output:
mathematica
Copy code
java
Copy code
interface Animal {
void bark();
@Override
public void bark() {
System.out.println("Dog is barking");
dog.bark();
Output:
csharp
Copy code
Dog is barking
java
Copy code
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
Output:
csharp
Copy code
java
Copy code
import java.io.File;
import java.io.FileNotFoundException;
try {
if (file.length() == 0) {
} catch (Exception e) {
Output:
php
Copy code
java
Copy code
System.out.println("Hello, IT B!");
}
public static void main(String[] args) {
thread.start();
Output:
css
Copy code
Hello, IT B!
java
Copy code
new EvenThread().start();
new OddThread().start();
}
Output:
makefile
Copy code
Even: 2
Odd: 1
...
java
Copy code
MultiplyThread(int num) {
this.num = num;
new MultiplyThread(5).start();
Output:
python
Copy code
5x1=5
5 x 2 = 10
...
java
Copy code
class Counter {
count++;
return count;
IncrementThread(Counter counter) {
this.counter = counter;
counter.increment();
}
}
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
Output:
yaml
Copy code
java
Copy code
import java.io.*;
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
Copy code
Hello World
Java Programming
java
Copy code
import java.io.*;
String line;
fileContent.append(line).append("\n");
} catch (IOException e) {
e.printStackTrace();
System.out.println(fileContent.toString());
}
}
Output:
Copy code
Hello World
Java Programming
java
Copy code
import java.io.*;
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Output:
arduino
Copy code
java
Copy code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
@Override
stage.setTitle("Greeting");
stage.setScene(scene);
stage.show();
launch(args);
java
Copy code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
stage.setTitle("Text Display");
stage.setScene(scene);
stage.show();
launch(args);
Output: Displays a window where text input updates a label upon button click.
java
Copy code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
addButton.setOnAction(e -> {
});
stage.setTitle("Basic Calculator");
stage.setScene(scene);
stage.show();
launch(args);
Copy code
class ElectricityBill {
consumerName = name;
connectionType = type;
previousReading = prev;
currentReading = curr;
double calculateBill() {
double bill = 0;
if ("domestic".equalsIgnoreCase(connectionType)) {
} else if ("commercial".equalsIgnoreCase(connectionType)) {
return bill;
Output:
yaml
Copy code
4o
You said:
from 39 to 43
ChatGPT said:
ChatGPT
Here are concise Java programs with their outputs for the requested questions:
java
Copy code
class Employee {
long mobileNo;
Employee(String name, String id, String addr, String mail, long mobile) {
empName = name;
empId = id;
address = addr;
mailId = mail;
mobileNo = mobile;
}
class Programmer extends Employee {
double basicPay;
Programmer(String name, String id, String addr, String mail, long mobile, double pay) {
basicPay = pay;
void generatePaySlip() {
p.generatePaySlip();
Output:
yaml
Copy code
java
Copy code
package mypackage;
File: Main.java
java
Copy code
import mypackage.MyClass;
obj.displayMessage();
Output:
vbnet
Copy code
java
Copy code
int a, b;
Shape(int a, int b) {
this.a = a;
this.b = b;
super(length, breadth);
void printArea() {
super(radius, unused);
void printArea() {
void printArea() {
rectangle.printArea();
circle.printArea();
triangle.printArea();
Output:
mathematica
Copy code
java
Copy code
import java.util.Random;
class RandomThread extends Thread {
while (true) {
if (num % 2 == 0) {
new SquareThread(num).start();
} else {
new CubeThread(num).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
int num;
SquareThread(int num) {
this.num = num;
}
class CubeThread extends Thread {
int num;
CubeThread(int num) {
this.num = num;
new RandomThread().start();
yaml
Copy code
Generated: 25
Generated: 10
java
Copy code
import java.io.*;
public class FileWriteRead {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
String line;
} catch (IOException e) {
e.printStackTrace();
Output:
css
Copy code