Q>4 w3 resourse
class Employee {
private int salary;
public Employee(int salary) {
this.salary = salary;
}
public void work() {
System.out.println("Employee is working");
}
public int getSalary() {
return salary;
}
}
class HRManager extends Employee {
public HRManager(int salary) {
super(salary);
}
@Override
public void work() {
System.out.println("HR Manager is managing employees");
}
public void addEmployee() {
System.out.println("HR Manager is adding a new employee");
}
}
// Main class
public class Main{
public static void main(String[] args) {
Employee emp = new Employee(40000);
HRManager mgr = new HRManager(70000);
emp.work();
System.out.println("Employee salary: " + emp.getSalary());
mgr.work();
System.out.println("Manager salary: " + mgr.getSalary());
mgr.addEmployee();
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Q>6
class Animal{
//method
public void move(){
System.out.println("Animal moves");
}
}
class cheetah extends Animal{
public void move(){
System.out.println("Cheetaj moves fater");
}
}
public class Main{
public static void main(String args[]){
Animal a=new Animal();
cheetah b= new cheetah();
a.move();
b.move();
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Q>7
class Person {
private String firstname;
private String lastname;
//constructor
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
//methods
public String getFirstName() {
return firstname;
}
public String getLastName() {
return lastname;
}
}
class Employee extends Person {
private int empid;
private String jobtitle;
public Employee(String firstname, String lastname, int empid, String jobtitle) {
super(firstname, lastname);
this.empid = empid;
this.jobtitle = jobtitle;
}
public int getEmpid() {
return empid;
}
public String getLastName() {
return super.getLastName() + ", " + jobtitle;
}
}
public class Main {
public static void main(String args[]) {
Employee employee1 = new Employee("Kortney", "Rosalee", 4451, "HR Manager");
System.out.println(employee1.getFirstName() + " " + employee1.getLastName() + " (" + employee1.getEmpid() + ")");
Employee employee2 = new Employee("Junior", "Philipa", 4452, "Software Manager");
System.out.println(employee2.getFirstName() + " " + employee2.getLastName() + " (" + employee2.getEmpid() + ")");
}
}
-----------------------------------------------------------------------------------------------------------------------------
Q>8
class Shape{
public double getPerimeter(){
return 0.0;
}
public double getArea(){
return 0.0;
}
}
class Circle extends Shape{
private double radius;
public Circle(double radius){
this.radius=radius;
}
public double getPerimeter(){
return Math.PI*radius*2;
}
public double getArea(){
return Math.PI*radius*radius;
}
}
public class Main{
public static void main(String args[]){
double r=8.0;
Circle c =new Circle(r);
System.out.println("radius of the circle"+r);
System.out.println("area of the circle" +c.getArea());
double r1=3.3;
Circle c1 =new Circle(r1);
System.out.println("radius of the circle"+r1);
System.out.println("area of the circle"+c1.getPerimeter());
}
}
---------------------------------------------------------------------------------------------------------------------------------
abstract
abstract class Shape3D{
public abstract double calculatevol();
public abstract double calsurfacearea();
}
class Sphere extends Shape3D{
private double radius;
public Sphere(double radius){
this.radius=radius;
}
public double calculatevol(){
return (4.0/3.0)*Math.PI*Math.pow(radius,3);
}
public double calsurfacearea(){
return 4.0*Math.PI*Math.pow(radius,2);
}
}
class Cube extends Shape3D{
private double sidelength;
public Cube(double sidelength) {
this.sidelength = sidelength;
}
public double calculatevol(){
return Math.pow(sidelength,3);
}
public double calsurfacearea(){
return 6*Math.pow(sidelength,2);
}
}
public class Main{
public static void main(String args[]){
Shape3D sphere=new Sphere(5.0);
Shape3D cube=new Cube(5.0);
System.out.println("sphere volume" + sphere.calculatevol());
System.out.println("Sphrer surface area" +sphere.calsurfacearea());
System.out.println("cube volume" + cube.calculatevol());
System.out.println("cube surface area" +cube.calsurfacearea());
}
}
---------------------------------------------------------------------------------------------
Q7>
abstract class Vehicle{
public abstract void StartEngine();
public abstract void StopEngine();
}
class Car extends Vehicle{
public void StartEngine(){
System.out.println("Start Engine of the car");
}
public void StopEngine(){
System.out.println("Stop");
}
}
class Bike extends Vehicle{
public void StartEngine(){
System.out.println("Start the BIke engine");
}
public void StopEngine(){
System.out.println("Stop");
}
}
public class Main{
public static void main(String args[]){
Vehicle car=new Car();
Vehicle bike=new Bike();
car.StartEngine();
bike.StartEngine();
car.StopEngine();
bike.StopEngine();
}
}
------------------------------------------------------------------------------------------------
Q>10
abstract class Shape2D{
public abstract void draw();
public abstract void resize();
}
class Rectangle extends Shape2D{
public void draw(){
System.out.println("Rectangle: Drawing a rectangle.");
}
public void resize(){
System.out.println("Rectangle: resize a rectangle.");
}
}
class Circle extends Shape2D{
public void draw(){
System.out.println("Circle: Drawing a Circle.");
}
public void resize(){
System.out.println("Circle: Drawing a Circle.");
}
}
public class Main{
public static void main(String args[]){
Shape2D rectangle= new Rectangle();
Shape2D circle =new Circle();
rectangle.draw();
rectangle.resize();
circle.draw();
circle.resize();
}
}
Q----------------LAB---------------------------------------------------------------------Q
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
class Rectangle extends Shape {
private double length;
private double breadth;
public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
@Override
public double calculateArea() {
return length * breadth;
}
@Override
public double calculatePerimeter() {
return 2 * (length + breadth);
}
}
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double calculatePerimeter() {
return side1 + side2 + side3;
}
}
public class Main {
public static void main(String[] args) {
double length = 4.0;
double breadth = 3.0;
Rectangle rectangle = new Rectangle(length, breadth);
double ts1 = 3.0, ts2 = 4.0, ts3 = 5.0;
Triangle triangle = new Triangle(ts1, ts2, ts3);
System.out.println("Length of the Rectangle: " + length);
System.out.println("Breadth of the Rectangle: " + breadth);
System.out.println("Area of the Rectangle: " + rectangle.calculateArea());
System.out.println("Perimeter of the Rectangle: " + rectangle.calculatePerimeter());
System.out.println("\nSides of the Triangle are: " + ts1 + ',' + ts2 + ',' + ts3);
System.out.println("Area of the Triangle: " + triangle.calculateArea());
System.out.println("Perimeter of the Triangle: " + triangle.calculatePerimeter());
}
}
-----------LAB------------------------------------------------------------------------
class Num {
protected int number;
public Num(int number) {
this.number = number;
}
public void shownum() {
System.out.println("Number: " + number);
}
}
class HexNum extends Num {
public HexNum(int number) {
super(number);
}
@Override
public void shownum() {
System.out.println("Hexadecimal Value: " + Integer.toHexString(number));
}
}
public class Main {
public static void main(String[] args) {
Num baseNum = new Num(42);
baseNum.shownum();
HexNum hexNum = new HexNum(42);
hexNum.shownum();
}
}
-
------------------------------------------------------------------------------------------------
import java.util.Scanner;
abstract class BankAccount {
abstract void deposit(double amount);
abstract void withdraw(double amount);
private double balance;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
class SavingsAccount extends BankAccount {
private double interestRate;
public SavingsAccount(double balance, double interestRate) {
setBalance(balance);
this.interestRate = interestRate;
}
@Override
void deposit(double amount) {
setBalance(getBalance() + amount);
}
@Override
void withdraw(double amount) {
setBalance(getBalance() - amount);
}
}
class CurrentAccount extends BankAccount {
public CurrentAccount(double balance) {
setBalance(balance);
}
@Override
void deposit(double amount) {
setBalance(getBalance() + amount);
}
@Override
void withdraw(double amount) {
setBalance(getBalance() - amount);
}
}
public class Main {
public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount(5000, 0.05);
savingsAccount.deposit(1000);
savingsAccount.withdraw(500);
System.out.println("Savings Account Balance: " + savingsAccount.getBalance());
CurrentAccount currentAccount = new CurrentAccount(10000);
currentAccount.deposit(2000);
currentAccount.withdraw(1000);
System.out.println("Current Account Balance: " + currentAccount.getBalance());
}
}
_______________________________________________________________________________________________
class Main{
public static void main(String args[]){
int vcount=0,ccount=0;
String str="manner dfg";
str=str.toLowerCase();
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u'){
vcount++;
}
//
else if(str.charAt(i)>='a'&& str.charAt(i)<='z'){
ccount++;
}
}
System.out.println("number of vowels"+vcount);
System.out.println("mumber of consonant"+ccount);
}
}
public class Main{
public static void main(String args[]){
String str="Dream big";
String reversedstr=" ";
for(int i=str.length()-1;i>=0;i--){
reversedstr=reversedstr+str.charAt(i);
}
System.out.println("original string"+str);
System.out.println("Reversed String"+reversedstr);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter