0% found this document useful (0 votes)
15 views

Java Lab3

The document contains code for rotating a 2D array in both clockwise and anticlockwise directions. It defines Clock, Clockwise, and Anticlockwise classes. The Clockwise class rotates the array clockwise by traversing in a clockwise spiral pattern. The Anticlockwise class rotates the array anticlockwise by recursively calling a rotation method, swapping the top-left element with its neighbors. Main tests both rotations by taking user input for the array dimensions and number of rotations.

Uploaded by

rosh na
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)
15 views

Java Lab3

The document contains code for rotating a 2D array in both clockwise and anticlockwise directions. It defines Clock, Clockwise, and Anticlockwise classes. The Clockwise class rotates the array clockwise by traversing in a clockwise spiral pattern. The Anticlockwise class rotates the array anticlockwise by recursively calling a rotation method, swapping the top-left element with its neighbors. Main tests both rotations by taking user input for the array dimensions and number of rotations.

Uploaded by

rosh na
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/ 20

1.import java.util.

*;
class Main
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int row=s.nextInt();
int col=s.nextInt();
int [][]a=new int[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
a[i][j]=s.nextInt();
}
}
int n=s.nextInt();
Clock cl=new Clock();
Clock cc=new Clockwise();
Clock ac=new Anticlockwise();
int [][]b=new int[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
b[i][j]=a[i][j];
}
}
Anticlockwise acw=new Anticlockwise();
for(int k=0;k<n;k++)
cc.rotate(row,col,a,n);
System.out.println("Clockwise ");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
System.out.print( a[i][j] + " ");
System.out.print("\n");
}
System.out.println("Anti clockwise ");
int r=row;
int c=col;
int k=n;
int f,K;
int l = 0;
int m = 0;
int Row = r-1;
int Col = c-1;
while(l < Row && m < Col)
{
int rot = 2*(Row-l)+2*(Col-m);
f = n%rot;
for(int i=1;i<=f;i++)
{
ac.rotate(row,col,b,n);

acw.rotation(l,m,Row,Col,b);
}
l++;
m++;
Row--;
Col--;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
System.out.print( b[i][j] + " ");
System.out.print("\n");
}

}
}
class Clock
{
public Clock()
{

}
void rotate(int row,int col,int a[][],int num)
{

}
}
class Clockwise extends Clock
{
static int R;
static int C;

void rotate(int m,int n,int mat[][],int num)


{
R = m;
C = n;

int row = 0, col = 0;


int prev, curr;
while (row < m && col < n)
{
if (row + 1 == m || col + 1 == n)
break;
prev = mat[row + 1][col];
for (int i = col; i < n; i++)
{
curr = mat[row][i];
mat[row][i] = prev;
prev = curr;
}
row++;
for (int i = row; i < m; i++)
{
curr = mat[i][n-1];
mat[i][n-1] = prev;
prev = curr;
}
n--;
if (row < m)
{
for (int i = n-1; i >= col; i--)
{
curr = mat[m-1][i];
mat[m-1][i] = prev;
prev = curr;
}
}
m--;
if (col < n)
{
for (int i = m-1; i >= row; i--)
{
curr = mat[i][col];
mat[i][col] = prev;
prev = curr;
}
}
col++;
}

}
}
class Anticlockwise extends Clock
{
void rotate(int m,int n,int mat[][],int num)
{}
void rotation(int l, int m, int Row, int Col,int mat[][])
{
int si,sj,i,j,t,f;
si = l;
sj = m;
t = mat[l][m];

for(i=l+1;i<=Row;i++)
{
f = mat[i][m];
mat[i][m] = t;
t = f;
}
l++;
for(i=m+1;i<=Col;i++)
{
f = mat[Row][i];
mat[Row][i] = t;
t = f;
}
m++;
if(l-1 < Row)
{
for(i=Row-1;i>=l-1;i--)
{
f = mat[i][Col];
mat[i][Col] = t;
t = f;
}
}
Col--;
if(m-1 < Col)
{
for(i=Col;i>=m;i--)
{
f = mat[l-1][i];
mat[l-1][i] = t;
t = f;
}
}
Row--;
mat[si][sj] = t;
return;
}

}
—------------------------------------------------
2.import java.io.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
class Event {
public String name;
public String detail;
public String type;
public String ownerName;
public Double costPerDay;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public Double getCostPerDay() {
return costPerDay;
}
public void setCostPerDay(Double costPerDay) {
this.costPerDay = costPerDay;
}
}

class Exhibition extends Event {


public int noOfStall;

public Exhibition(String name, String detail, String type, String ownerName,


Double costPerDay, int noOfStall2) {
this.name = name;
this.detail = detail;
this.type = type;
this.ownerName = ownerName;
this.costPerDay = costPerDay;
this.noOfStall = noOfStall;
}

public Exhibition() {
this.name = null;
this.detail = null;
this.type = null;
this.ownerName = null;
this.costPerDay = (double) 0;
this.noOfStall = 0;
}

public int getNoOfStall() {


return noOfStall;
}

public void setNoOfStall(int noOfStall) {


this.noOfStall = noOfStall;
}
}
class StageEvent extends Event {
public int noOfSeats;

public StageEvent(String name, String detail, String type, String ownerName,


Double costPerDay, int noOfSeats2) {
this.name = name;
this.detail = detail;
this.type = type;
this.ownerName = ownerName;
this.costPerDay = costPerDay;
this.noOfSeats = noOfSeats;
}

public StageEvent() {
this.name = null;
this.detail = null;
this.type = null;
this.ownerName = null;
this.costPerDay = 0.0;
this.noOfSeats = 0;
}

public int getNoOfSeats() {


return noOfSeats;
}
public void setNoOfSeats(int noOfSeats) {
this.noOfSeats = noOfSeats;
}
}
class Main {
public static void main(String[] args) throws ParseException {
int n;
Scanner sc = new Scanner(System.in);
n=Integer.parseInt(sc.nextLine());
Exhibition ex = new Exhibition();
StageEvent se = new StageEvent();
DecimalFormat dd = new DecimalFormat("0.0");
if(n==1) {
ex.setName(sc.nextLine());
ex.setDetail(sc.nextLine());
ex.setType(sc.nextLine());
ex.setOwnerName(sc.nextLine());
ex.setCostPerDay(Double.parseDouble(sc.nextLine()));
ex.setNoOfStall(Integer.parseInt(sc.nextLine()));
Exhibition ex1 = new
Exhibition(ex.name,ex.detail,ex.type,ex.ownerName,ex.costPerDay,ex.noOfStall);
String date1 = sc.nextLine();
String date2 = sc.nextLine();
Date start = new SimpleDateFormat("dd/MM/yyyy").parse(date1);
Date end = new SimpleDateFormat("dd/MM/yyyy").parse(date2);
long diff = (start.getTime()-end.getTime())/86400000;
Double result = 0.05*ex.costPerDay*diff;
System.out.println(dd.format(Math.abs(result)));
}
if(n==2) {
se.setName(sc.nextLine());
se.setDetail(sc.nextLine());
se.setType(sc.nextLine());
se.setOwnerName(sc.nextLine());
se.setCostPerDay(Double.parseDouble(sc.nextLine()));
se.setNoOfSeats(Integer.parseInt(sc.nextLine()));
StageEvent se1 = new
StageEvent(se.name,se.detail,se.type,se.ownerName,se.costPerDay,se.noOfSeats);
String date1 = sc.nextLine();
String date2 = sc.nextLine();
Date start = new SimpleDateFormat("dd/MM/yyyy").parse(date1);
Date end = new SimpleDateFormat("dd/MM/yyyy").parse(date2);
long diff = (start.getTime()-end.getTime())/86400000;
Double result = 0.15*se.costPerDay*diff;
System.out.println(dd.format(Math.abs(result)));
}
}
}
—---------------------------------------------------------------------------------------
3.import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

abstract class Account{


String name;
int number;
int balance;
Date startDate;

public Account(String name, int number, int balance, Date startDate) {


this.name = name;
this.number = number;
this.balance = balance;
this.startDate = startDate;
}

abstract public double calculateInterest(Date dueDate);

class CurrentAccount extends Account{

public CurrentAccount(String name, int number, int balance, Date startDate) {


super(name, number, balance, startDate);

public double calculateInterest(Date dueDate) {

double interest;
interest = (balance * 5 * (monthsDifference(startDate, dueDate)/12))/100;

return interest;
}

public int monthsDifference(Date startDate, Date endDate) {


Calendar c1 = new GregorianCalendar();
c1.setTime(startDate);
Calendar c2 = new GregorianCalendar();
c2.setTime(endDate);
int ans = (c2.get(c2.YEAR) - c1.get(c1.YEAR))*12;
ans += c2.get(c2.MONTH)-c1.get(c1.MONTH);
return ans;
}
}

class SavingsAccount extends Account{

public SavingsAccount(String name, int number, int balance, Date startDate) {


super(name, number, balance, startDate);

public double calculateInterest(Date dueDate) {

double interest;
interest = (balance * 12 * (monthsDifference(startDate, dueDate)/12))/100;

return interest;
}

public int monthsDifference(Date startDate, Date endDate) {


Calendar c1 = new GregorianCalendar();
c1.setTime(startDate);
Calendar c2 = new GregorianCalendar();
c2.setTime(endDate);
int ans = (c2.get(c2.YEAR) - c1.get(c1.YEAR))*12;
ans += c2.get(c2.MONTH)-c1.get(c1.MONTH);
return ans;
}

class AccountsMain{
public static void main(String args[]) throws ParseException {
Scanner myObj = new Scanner(System.in);

int type=Integer.parseInt(myObj.nextLine());
String name = myObj.nextLine();
int number=Integer.parseInt(myObj.nextLine());
int balance= Integer.parseInt(myObj.nextLine());

String dateString = myObj.nextLine();


DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date startDate = formatter.parse(dateString);
dateString = myObj.nextLine();
Date dueDate = formatter.parse(dateString);

if(type==1) {
SavingsAccount sObj = new SavingsAccount(name, number, balance,
startDate);
System.out.println(sObj.calculateInterest(dueDate));
}

else {
CurrentAccount cObj = new CurrentAccount(name, number, balance,
startDate);
System.out.println(cObj.calculateInterest(dueDate));
}
myObj.close();
}

}
—------------------------------------------
4.import java.io.*;
import java.util.*;
class Event {
protected String name;
protected String detail;
protected String type;
protected String organiserName;
public Event(String name, String detail, String type, String organiserName) {
this.name = name;
this.detail = detail;
this.type = type;
this.organiserName = organiserName;
}
public Event() {
this.name = null;
this.detail = null;
this.type = null;
this.organiserName = null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOrganiserName() {
return organiserName;
}
public void setOrganiserName(String organiserName) {
this.organiserName = organiserName;
}
}
class Exhibition extends Event {
private int noOfStalls;

public Exhibition(String name, String detail, String type,


String organiserName, String noOfStalls) {
super(name,detail,type,organiserName);
this.noOfStalls = Integer.parseInt(noOfStalls);

}
public Exhibition() {
super();
this.noOfStalls = 0;
}
public int getNoOfStalls() {
return noOfStalls;
}

public void setNoOfStalls(int noOfStalls) {


this.noOfStalls = noOfStalls;
}
}
class StageEvent extends Event {
private int noOfSeats;

public StageEvent(String name, String detail, String type,


String organiserName, String noOfSeats) {
super(name,detail,type,organiserName);
this.noOfSeats = Integer.parseInt(noOfSeats);
}
public StageEvent() {
super();
this.noOfSeats = 0;
}
public int getNoOfSeats() {
return noOfSeats;
}

public void setNoOfSeats(int noOfSeats) {


this.noOfSeats = noOfSeats;
}
}
class Main {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
n = Integer.parseInt(sc.nextLine());
if(n==1) {
String detail = sc.nextLine();
String [] exhib = detail.split(",");
Event e = new Event(exhib[0],exhib[1],exhib[2],exhib[3]);
Exhibition ex = new Exhibition(exhib[0],exhib[1],exhib[2],exhib[3],exhib[4]);
System.out.println(ex.name+" "+ex.detail+" "+ex.type+"
"+ex.organiserName+" "+ex.getNoOfStalls());
}
if(n==2) {
String detail = sc.nextLine();
String [] stage = detail.split(",");
Event e = new Event(stage[0],stage[1],stage[2],stage[3]);
StageEvent se = new
StageEvent(stage[0],stage[1],stage[2],stage[3],stage[4]);
System.out.println(se.name+" "+se.detail+" "+se.type+"
"+se.organiserName+" "+se.getNoOfSeats());
}
if(n>=3){
System.out.println("Invalid input");
}
}
}
—--------------------------------------------------------------
5.import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
class Shape {
protected double area;

public void computeArea() {


area =0 ;
}

public double getArea() {


return area;
}

public void setArea(double area) {


this.area = area;
}
}
class Circle extends Shape {
private double radius;

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}
public void computeArea() {
DecimalFormat d = new DecimalFormat("0.00");
this.area = 3.14*(radius*radius);
System.out.println(d.format(this.area));
}
}
class Rectangle extends Shape {
private double length;
private double breadth;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public void computeArea() {
DecimalFormat d = new DecimalFormat("0.00");
this.area = length*breadth;
System.out.println(d.format(this.area));
}
}
class Triangle extends Shape {
private double base;
private double height;
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public void computeArea() {
DecimalFormat d = new DecimalFormat("0.00");
this.area = 0.5*base*height;
System.out.println(d.format(this.area));
}
}
class Main {
public static void main(String [] args) {
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Circle c = new Circle();
Rectangle r = new Rectangle();
Triangle t = new Triangle();
if(n == 1) {
c.setRadius(sc.nextDouble());
c.computeArea();
}
if(n == 2) {
r.setLength(sc.nextDouble());
r.setBreadth(sc.nextDouble());
r.computeArea();
}
if(n == 3) {
t.setBase(sc.nextDouble());
t.setHeight(sc.nextDouble());
t.computeArea();
}
if(n>3) {
System.out.println("Invalid Input");
}
}
}

—-----------------------------------------------------------
6.import java.util.Scanner;
class Person{
private String name;
private int birthYear;

public Person(String n, int byear){


name = n;
birthYear = byear;
}

public String toString() {


return "Name : " + name + "\nBirthYear : " + birthYear + "";
}
}
class Staff extends Person{
protected double salary;

public Staff(String n, int byear, double s) {


super(n, byear);
salary = s;
}

public String toString(){


return super.toString() + "\nOld Salary : " + salary ;
}
}
class Student extends Person{
private String major;
private double att;

public Student(String n, int byear, String m,double at) {


super(n, byear);
major = m;
att=at;
}

public String atten(double att) {


return (att>=75)?"Yes":"No";
}

public String toString() {


return super.toString() + "\nDepartment : " + major +"\nEligible : "+ atten(att);
}
}

class TeachingStaff extends Staff{


private String subject;
private double result;
public TeachingStaff(String n, int byear, double s,String sub,double res) {
super(n, byear,s);
subject = sub;
result = res;
}

double salCalc() {
return salary+(salary*(result/1000));

public String toString(){


return super.toString() + "\nSubject : " + subject + "\nNew Salary : "+salCalc();
}
}

class NonTeachingStaff extends Staff{


private String lab;
private double exp;
public NonTeachingStaff(String n, int byear, double s,String l,double expr) {
super(n, byear,s);
lab = l;
exp=expr;
}

double ntsalCalc() {
return salary+(salary*(exp/100));

public String toString(){


return super.toString() + "\nLab : " + lab+ "\nNew Salary : "+ntsalCalc();
}
}

class PersonTester{
public static void main(String[] args){

Scanner myObj =new Scanner(System.in);


int choice = Integer.parseInt(myObj.nextLine());
String name =myObj.nextLine();
int byear=Integer.parseInt(myObj.nextLine());

if(choice ==1) {
String dept = myObj.nextLine();
double att =myObj.nextDouble();
Student b = new Student(name, byear, dept,att);
System.out.println(b);
}
else if(choice==2) {
String sub = myObj.nextLine();
double result =myObj.nextDouble();
double sal =myObj.nextDouble();
TeachingStaff ts = new TeachingStaff(name, byear, sal,sub,result);
System.out.println(ts);
}
else if(choice ==3) {
String lab = myObj.nextLine();
double exp =myObj.nextDouble();
double sal =myObj.nextDouble();
NonTeachingStaff nts = new NonTeachingStaff(name, byear, sal,lab,exp);
System.out.println(nts);
}

}
}
—--------------------------------------------
7.import java.io.*;
import java.util.*;
class Account {
protected String accountNumber;
protected double balance;
protected String accountHoldername;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getAccountHoldername() {
return accountHoldername;
}
public void setAccountHoldername(String accountHoldername) {
this.accountHoldername = accountHoldername;
}
}
class SavingAccount extends Account {
protected double minimumBalance;

public double getMinimumBalance() {


return minimumBalance;
}

public void setMinimumBalance(double minimumBalance) {


this.minimumBalance = minimumBalance;
}
}
class FixedAccount extends SavingAccount {
private int lockingPeriod;

public int getLockingPeriod() {


return lockingPeriod;
}

public void setLockingPeriod(int lockingPeriod) {


this.lockingPeriod = lockingPeriod;
}
}
class AccountBO {
public void getAccountDetail(String detail) {
String [] bank = detail.split(",");
System.out.format("%-20s %-10s %-20s %-20s %s\n","Account
Number","Balance","Account holder name","Minimum balance","Locking period");
System.out.format("%-20s %-10s %-20s %-20s
%s\n",bank[0],bank[1],bank[2],bank[3],bank[4]);
}
}
class Main {
public static void main(String [] args) {
String detail;
Scanner sc = new Scanner(System.in);
detail = sc.nextLine();
AccountBO ab = new AccountBO();
ab.getAccountDetail(detail);

}
}
—-----------------------------------------
8.import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
class VISACard {
private String holderName;
private String ccv;
public Double computeRewardPoints(String purchaseType, Double amount) {
double dis = 0.01*amount;
return dis;
}
public String getHolderName() {
return holderName;
}
public void setHolderName(String holderName) {
this.holderName = holderName;
}
public String getCcv() {
return ccv;
}
public void setCcv(String ccv) {
this.ccv = ccv;
}

}
class HPVISACard extends VISACard {
public Double computeRewardPoints(String purchaseType, Double amount) {
double dis = super.computeRewardPoints(purchaseType, amount);
if(purchaseType.compareTo("fuel") == 0) {
return dis+10;
}
else {
return dis;
}

}
}

class Main {
public static void main(String [] args) {
VISACard v = new VISACard();
HPVISACard h = new HPVISACard();
DecimalFormat d = new DecimalFormat("0.0");
Scanner sc = new Scanner(System.in);
v.setHolderName(sc.nextLine());
v.setCcv(sc.nextLine());
Double amount = Double.parseDouble(sc.nextLine());
String type = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
if(n == 1) {
double res = v.computeRewardPoints(type, amount);
System.out.println(d.format(res));
}
if(n == 2) {
double res = h.computeRewardPoints(type, amount);
System.out.println(d.format(res));
}
if(n>2) {
System.out.println("Invalid Choice");
}

}
}

—--------------------------------------------------
9.public static void main(String s){
System.out.println("Overloaded: "+s);
}

public static void main(String s1,String s2){


System.out.println("Overloaded: "+s1+" & "+s2);
}
—------------------------------
10.class Dog extends Animal {

void Print()
{
System.out.println("Dog");
}
}

class Cat extends Animal {

void Print()
{
System.out.println("Cat");
}
}

You might also like