0% found this document useful (0 votes)
1 views68 pages

Import Java

The document contains multiple Java class implementations for various functionalities, including string manipulation, binary to decimal conversion, salary calculation, shopping cart management, password checking, and library management. Each class features a main method that interacts with the user via the console, allowing for input and displaying results based on the operations performed. Overall, the document serves as a collection of small Java programs demonstrating different programming concepts and user interactions.

Uploaded by

lmg.ashmitadas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views68 pages

Import Java

The document contains multiple Java class implementations for various functionalities, including string manipulation, binary to decimal conversion, salary calculation, shopping cart management, password checking, and library management. Each class features a main method that interacts with the user via the console, allowing for input and displaying results based on the operations performed. Overall, the document serves as a collection of small Java programs demonstrating different programming concepts and user interactions.

Uploaded by

lmg.ashmitadas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 68

import java.util.

*;

public class STRINGG

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a sentence: ");

String s = scanner.nextLine();

int dC = 0;

int sC = 0;

int cC = 0;

int aC = 0;

int spC = 0;

int vC = 0;

int wC = 1;

for (int i = 0; i < s.length(); i++) {

char ch = s.charAt(i);

if (Character.isDigit(ch)) {

dC++;

} else if (Character.isLowerCase(ch)) {

sC++;

aC++;

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

vC++;

} else if (C…
import java.util.*;

class STRINGGG

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter a word:");

String str= sc.nextLine();

for (int i = 0; i < str.length(); i++)

c = str.charAt(i);

if ((i == 0 && Character.isLetter(c)))

if ( c == 'E')

String mod = word.substring(0, str.length() - 2) + "O";

System.out.println("Modified word: " + mod);

else

System.out.println("The word doesn't start with 'E'. No modification applied.");

import java.util.*;

class STRINGGGG
{

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter a sentence:");

String s = sc.nextLine();

String str = "";

for (int i = 0; i < s.length(); i++)

char ch = s.charAt(i);

if (Character.isLetter(ch))

if (ch >= 'A' && ch <= 'Z') {

ch = (char) ('A' + (ch - 'A' + 2) % 26);

else if (ch >= 'a' && ch <= 'z')

ch = (char) ('a' + (ch - 'a' + 2) % 26);

str += ch;

System.out.println("Modified sentence: " + str);

import java.util.*;

class BinaryToDecimal
{

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int b,i,j=0,k;

double sum=0.0;

System.out.println("enter the binary digit");

b=sc.nextInt();

while (b!=0)

i=(b%10);

sum=(sum + (i*Math.pow(2,j)));

b=(b/10);

j+=1;

System.out.println("The decimal number = " + (int)sum);

/**

3. Define a class SalaryCalculation described as below:

Data members:

name (String data type)

basicPay, specialAlw, conveyanceAlw, gross, pf, netSalary, annualSal (double data types)

Member methods:

a) SalaryCalculation(): A constructor to assign name of employee(name),basic


salary(basicPay),conveyance allowance (conveyanceAlw) as Rs. 1000.00.

b) void salaryCal(): To calculate other allowances and salaries as given below:

specialAlw = 25% of basic salary


pf = 11% of basic salary

gross = basicPay + specialAlw + conveyanceAlw

netSalary = gross – pf

annualSal = 12 months of net salary

c) void display(): To display the name and other calculations with suitable headings.

Write a main() method to call the above member methods.

*/

import java.util.*;

class Salary…

import java.util.*;

class ShoppingCart

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

String cart[] = new String[10];

int c = 0;

while (true)

System.out.println("|| Shopping Cart Menu ||");

System.out.println("1. Add Item to Cart");

System.out.println("2. View Cart");

System.out.println("3. Exit");

System.out.print("Choose an option: ");

int ch = sc.nextInt();

sc.nextLine(); // consume the newline character


switch (ch)

case 1:

if (c < cart.length)

do

import java.util.*;

class RestaurantMenu

public static void main(String args[])

Scanner sc = new Scanner(System.in);

String[] menu = {"Burger", "Pizza", "Pasta", "Salad", "Soda"};

double[] prices = {298, 325, 349, 239, 175};

String[] order = new String[10];

double[] orderPrices = new double[10];

int orderCount = 0;

while (true)

System.out.println("\n--- Restaurant Menu ---");

for (int i = 0; i < menu.length; i++)

System.out.println((i + 1) + ". " + menu[i] + " - Rs." + prices[i]);

System.out.println("6. View Order");


System.out.println("7. Exit");

System.out.print("Choose an…

import java.util.*;

public class PasswordChecker

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter a password to check: ");

String password = sc.nextLine();

if(isStrongPassword(password))

System.out.println("Password is strong.");

else

System.out.println("Password is weak.");

sc.close();

public static boolean isStrongPassword(String password)

if(password.length() < 8)

return false;

boolean hasUpper = false;

boolean hasLower = false;

boolean hasDigit = false;


boolean hasSpecial = false;

for(ch…

import java.util.*;

class CountdownTimer

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int time;

long s = 2000200000;

System.out.print("Enter the number of seconds required for countdown: ");

time = sc.nextInt();

while (time > 0)

for (int i = 1; i <= time && time > 0 ; i++)

System.out.println(time + " seconds");

time--;

for (long j = 1; j <= s ;j++)

System.out.println("Time's up!");

}
}

/**

A class Iscscores defines the scores of a candidate in six subjects and another

class bestfour defines the best four subjects.

The details of both the classes are given below:

Class name :

Iscscores

Data members/instance variables:

int number[6][2]

to contain marks for six subjects and subject code[numeric]

Member functions/methods

Iscscores() :

constructor to accept the marks.

int point() :

to return the point in each subject according to the following:

Marks >= 90 1 point

80-89 2 point

70-79 3 point

60-69 4 point

50-59 5 point

40-99 6 point

-accordingly.

Class name

bestfour

Member functions/methods

void bestsubjects(): to display the total points and best four subject codes using the concept
of inh…

/**
6. Define a class PhoneBill with the following descriptions:

Data members:

customerName of String type

phoneNo of long type

no_of_unit of int type

rent of int type

amount of double type

Methods:

PhoneBill(): to initialize with default value to each data member.

accept(): to accept customer name, phone number, number of units and rent.

calculate(): to calculate the amount as rent + cost for the units, where cost for the units

is to be calculated according to the following conditions:

First 50 calls: Free

Next 100 calls: Rs. 0.80/unit

Next 200 calls: Rs. 1.00/unit

Remaining calls: Rs. 1.20/unit

display(): to display the values of all the data members on the screen.

*/

import java.util.*;

class PhoneBill

String customerName;

long phoneNo;

int n…

import java.util.*;

public class LibraryManagement

public static void main(String[] args)


{

Scanner sc = new Scanner(System.in);

String[] books = new String[10];

int bookCount = 0;

while (true)

System.out.println("\nLibrary Management System");

System.out.println("1. Add Book");

System.out.println("2. Remove Book");

System.out.println("3. List Books");

System.out.println("4. Exit");

System.out.print("Choose an option: ");

int ch = sc.nextInt();

sc.nextLine(); // Consume the newline character

switch (ch)

case 1:

if (bookCount < books.length)

import java.util.*;

public class UserAuthentication

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int correctPassword = 378;

int enteredPassword;

int attempts = 0;
int maxAttempts = 3;

do

System.out.print("Enter your password: ");

enteredPassword = sc.nextInt();

if (enteredPassword==correctPassword)

System.out.println("Authentication successful!");

break;

else

attempts++;

System.out.println("Incorrect password. Attempts remaining: " + (maxAttempts -


attempts));

while (…

import java.util.*;

public class ContactSearch

public static void main(String[] args)

String[] contacts = {"Harry", "Bob", "Hansel", "David", "Chaitali", "Vincent", "Sandipta",


"Jyotiraditya"};

Scanner sc = new Scanner(System.in);

System.out.print("Enter the name to find: ");


String nameToFind = sc.nextLine();

boolean found = false;

for(String contact : contacts)

if(contact.equals(nameToFind))

found = true;

break;

if(found) {

System.out.println(nameToFind + " is in the contact list.");

} else {

System.out.println(nameToFind + " is not in the contact list.");

/**

Write a program in Java to store 10 numbers (including positive and negative numbers) in a
Single Dimensional Array (SDA).

Display all the negative numbers followed by the positive numbers without changing the
order of the numbers.

*/

import java.util.*;

class POS_NEG

public static void main(String args[])

{
Scanner sc = new Scanner(System.in);

int a[] = new int[10];

for (int i=0; i<10; i++)

a[i] =sc.nextInt();

for (int i=0; i<10; i++)

if (a[i] < 0)

System.out.print(a[i] + " ");

for (int i = 0; i < 10; i++) {

if (a[i] > 0)

System.out.print(a[i] + " ");

import java.util.*;

public class SimpleATM

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

double balance = 1000.00;

while (true)

System.out.println("\n--- ATM Menu ---");


System.out.println("1. Check Balance");

System.out.println("2. Deposit Money");

System.out.println("3. Withdraw Money");

System.out.println("4. Exit");

System.out.print("Choose an option: ");

int ch = sc.nextInt();

switch (ch)

case 1:

System.out.println("Your current balance is: Rs." + balance);

break;

case 2:

System.out.prin…

/**

5. Accept numbers into an integer array A of size 4, create another string array B of size 3.

Accept any three mathematical symbols like (+, -, *, /) in array B. Then according to
mathematical symbol calculate the answer.

For e.g. if A = [8, 6, 4, 2] & B = [+, -, *], then answer will be (((8 + 6) - 4) * 2) = 20

*/

import java.util.*;

class Calculation

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int A[] = new int[4];

char B[] = new char[3];


for (int i = 0; i < 4; i++)

A[i] = sc.nextInt();

for (int i = 0; i < 3; i++)

B[i] = sc.next().charAt(0);

int result = A[0];

for (int i = 0; i…

/**

Create a class named Pizza that stores details about a pizza. It should contain the following:

Instance Variables:

String pizzaSize — to store the size of the pizza (small, medium, or large)

int cheese — the number of cheese toppings

int pepperoni — the number of pepperoni toppings

int mushroom — the number of mushroom toppings

Member Methods:

Constructor — to initialise all the instance variables

CalculateCost() — A public method that returns a double value, that is, the cost of the pizza.

Pizza cost is calculated as follows:

Small: Rs.500 + Rs.25 per topping

Medium: Rs.650 + Rs.25 per topping

Large: Rs.800 + Rs.25 per topping

PizzaDescription() — A public method that returns a String containing


1) the pizza size,

2) quantity of each topping

3) the …

import java.util.*;

public class MovieTicketBooking

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

String[] movies = {"Movie A", "Movie B", "Movie C"};

double[] prices = {250.00, 312.00, 615.00};

int[] ticketsBooked = new int[3];

while (true)

System.out.println("\n--- Movie Ticket Booking ---");

for (int i = 0; i < 3; i++)

System.out.println((i + 1) + ". " + movies[i] + " - Rs." + prices[i]);

System.out.println((4) + ". View Booking");

System.out.println((5) + ". Exit");

System.out.print("Choose an option: ");

int ch = sc.nextInt();

swit…

import java.util.*;

class sel
{

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int min;

System.out.println("Enter the size of the array:");

int s = sc.nextInt();

sc.nextLine();

String a[] = new String[s];

System.out.println("Enter " + s + " elements:");

for (int i = 0; i < s; i++)

a[i] = sc.nextLine();

for (int i = 0; i < (s - 1); i++)

min = i;

for (int j = i + 1; j < s; j++)

if (a[j].compareTo(a[min]) < 0)

min = j;

String temp = a[i];

a[i] = a[min…

/**
2. Define a class named four Side, having members as:

Data members: length, breadth

Member Functions:

i. Overloaded constructor to initialize the dimension of the four-sided figure with a

square and a rectangle.

ii. Compute the area and display it.

Also create the main() method to show the implementation of the above methods.

*/

import java.util.*;

class FourSide

double length, l, b, breadth;

public FourSide(double side)

length = side;

breadth = side;

public FourSide(double l, double b) {

length = l;

breadth = b;

public void area() {

double area = length * breadth;

System.out.println("The area is: " + area);

public static void main(String[] args)


import java.util.*;

class Author

String name;

String nationality;

public Author(String n, String na)

name = n;

nationality = na;

public void displayAuthorInfo() {

System.out.println("Author: " + name);

System.out.println("Nationality: " + nationality);

class BookList extends Author

String books[];

int bookCount;

public BookList(String n, String na, int maxBooks)

super (n, na);

books = new String[maxBooks];

bookCount = 0;
}

public void addBook(String bookTitle) {

if (bookCount < books.length) {

books[bookCount] = bookTitle;

bookCount++;

System.out.println("Book '" + bookTitle + "' added …

import java.util.*;

class CopyOfShoppingCart

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

String cart[] = new String[10];

int c = 0;

while (true)

System.out.println("|| Shopping Cart Menu ||");

System.out.println("1. Add Item to Cart");

System.out.println("2. View Cart");

System.out.println("3. Exit");

System.out.print("Choose an option: ");

int ch = sc.nextInt();

sc.nextLine(); // consume the newline character

switch (ch)

{
case 1:

do

System.out.print("Enter item name: ");

import java.util.*;

class Overload

static void series(int n)

int s =0, c=0;

for (int i =1; i <=n; i++)

int a = 2;

for(int j = 1; j<=a; j++)

if (a%j==0)

c++;

if (c==2)

s+=a;

a++;

System.out.println("Sum = " + s);


}

static void series()

int a = 5, b = 10, c;

c = (a+b);

System.out.println( a + " " + b + " ");

for ( int i = 1; i<=10; i++)

a = b;

b = c;

System.out.println(c + " ");

static void series(int …

static void series(int x, int n)

int s = 0;

for (int j = 1; j <= (2 * n - 1); j += 2)

if (j % 4 == 1) {

s += Math.pow(x, j);

} else {

s -= Math.pow(x, j);

System.out.println("Sum = " + s);

}
//2. Write a function in java to accept a String and find the frequency of a word entered by
the user in a string.

import java.util.*;

class frequency

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int f = 0;

System.out.println("Enter a string");

String str = sc.nextLine();

System.out.println("Enter a word to be compared");

String s = sc.next();

str = str + " ";

String t = "";

for (int i = 0; i < str.length(); i++)

char ch = str.charAt(i);

if (ch != ' ')

t = t + ch;

else

if (t.compareTo(s) == 0)


import java.util.*;

class encode

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string");

String s = sc.nextLine();

String str = " ";

int l = s.length();

for(int i = 0; i < l ; i++)

char ch = s.charAt(i);

if ( ch != ' ')

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

str = str + (char)(ch+1);

} else

str = str + ch;

else

str = str + " ";

}
}

System.out.println(str);

import java.util.*;

class j2015_Parkinglot

int vno, hours;

double bill;

public void input(int v, int h)

vno = v;

hours = h;

public void calculate()

hours--;

bill = 3 + (1.5*hours);

public void display()

System.out.println("The vehicle number = " + vno);

System.out.println("The number of hours = " + (++hours));

System.out.println("The parking fee = " + bill);

public static void main(String args[])

Scanner sc = new Scanner(System.in);


System.out.println("Enter the vehicle number");

int v = sc.nextInt();

System.out.println("Enter the number of hours");

int h = sc.nextInt();

j2015_Parkinglot o…

import java.util.*;

class j2015_Pattern

public static void main(String args[])

for (int i = 1; i<=5; i++)

for (int j = 1; j<=i; j++)

if (j%2==0)

System.out.print("#" + " ");

else

System.out.print("*" + " ");

System.out.println();

for (int i = 1; i <= 5; i++) {

for (int j = 5; j >= i && j > 0; j--) {

System.out.print(j + " ");

System.out.println();

}
}

import java.util.*;

class j2015_factorial {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Choose");

System.out.println("1. Find factors of the number. 2. Find factorial of the number.");

int ch = sc.nextInt();

switch (ch) {

case 1:

System.out.println("Enter the number");

int n = sc.nextInt();

System.out.println("Factors are ");

for (int i = 1; i<n; i++){

if (n%i==0)

System.out.print(i + " ");

break;

case 2:

System.out.println("Enter the number");

int num = sc.nextInt();

int f =1;

for (int i = 1; i<=num; i++){

f = f*i;

System.out.println("Factorial = " + f);


break;

default:

System.out.println("Invalid Option");

break;

import java.util.*;

class j2015_Array {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

String[] names = new String[20];

System.out.println("Enter the names:");

for (int i = 0; i < 20; i++) {

names[i] = sc.nextLine();

for (int i = 0; i < 20 - 1; i++) {

for (int j = 0; j < 20 - i - 1; j++) {

if (names[j].compareTo(names[j + 1]) < 0)

String t = names[j];

names[j] = names[j + 1];

names[j + 1] = t;

System.out.println("Names in descending order:");


for (String name : names)

System.out.println(name);

/**

Define a class called 'Mobike' with the following specifications:

Data Members Purpose

int bno To store the bike number

int phno To store the phone number of the customer

String name To store the name of the customer

int days To store the number of days the bike is taken on rent

int charge To calculate and store the rental charge

Member Methods Purpose

void input() To input and store the details of the customer

void compute() To compute the rental charge

void display() To display the details in the given format

The rent for a mobike is charged on the following basis:

Days Charge

For first five days ₹500 per day

For next five days ₹400 per day

Rest of the days ₹200 per day

Output:
Bike No. Phone No. Name No. of days …

/**

IC

ICS

ICSE

*/

class Pattern {

public static void main(String args[]) {

String s = "ICSE";

int l = s.length();

for (int i = 1; i <= l; i++) {

System.out.println(s.substring(0, i));

//Write a function in java to accept a String and find the frequency of each character

import java.util.Scanner;

class CharFreq {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter any sentence:");

String s = sc.nextLine();

int l = s.length();

char[] ch = new char[l];

int c = 1;
for (int i = 0; i < l; i++) {

ch[i] = s.charAt(i);

for (int i = 0; i < l - 1; i++) {

for (int j = 0; j < l - 1 - i; j++) {

if (ch[j] > ch[j + 1]) {

char temp = ch[j];

ch[j] = ch[j + 1];

ch[j + 1] = temp;

import java.util.*;

class UniqueNum

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int r1, r2, n, n1, n2, c=0;

System.out.println("Enter the number");

n = sc.nextInt();

n1 = n;

n2 = n;

while (n1!=0)

r1 = (n1%10);
while (n2!=0)

r2 = (n2%10);

if (r1==r2)

c++;

n2 = (n2/10);

n1=(n1/10);

if (c==1)

System.out.println("The number is unique.");

else

System.out.println("The number is not unique.");

import java.util.*;

class AreaCalculator {

double area(int s) {

return s*s;

double area(int l, int w) {

return l*w;
}

double area(double r) {

return 3.14*r*r;

double area(double b, double h) {

return 0.5*b*h;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

AreaCalculator cal = new AreaCalculator();

System.out.println("Choose");

System.out.println("1. Area of Square");

System.out.println("2. Area of Rectangle");

System.out.println("3. Area of Circle");

System.out.println("4. Area of Triangle");

int ch = sc.nextInt();

switch (ch)

import java.util.*;

class Series1 {

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

int s = 0;

for (int i = 10; i>=1; i--) {

s+=i;

}
for (int j = 1; j<=10; j++) {

s-=j;

System.out.println("The sum of series = " + s);

import java.util.*;

class Series2 {

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

double s = 0.0, p = 0.0;

for (int i = 1; i<=6; i++) {

p= (p*10)+5;

s=p/(Math.pow(10,i));

System.out.print(s + " ");

/**

Write a program to display all Mersenne numbers between 1... n using the method name
MersenneNos (int n) where n has to be in the range 1-20.

[Mersenne numbers are the ones which are one less than the power of 2]

*/

import java.util.*;

class Mersenne {

void MersenneNos(int n) {

System.out.println("Mersenne numbers between 1 and " + n + ":");

for (int i = 1; i <= n; i++) {


int p = (int) Math.pow(2, i);

int m = p - 1;

System.out.println(m);

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number (1-20): ");

int n = sc.nextInt();

Mersenne obj = new Mersenne();

obj.MersenneNos(n);

/**

An electricity board charges the bill depending on the number of units consumed as follows:

Units Consumed Rate per unit

First 100 units 40p. per unit

Next 200 units 60p. per unit

Above 300 units Rs.1 per unit

*/

import java.util.*;

class EBill {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of units consumed");

int u = sc.nextInt();

double bill = 0.0;

if ( u >= 100)
bill = (u*40);

else if (u < 100 && u >= 300)

bill = (100*40) + ((u-100)*60);

else

bill = (100*40) + (200*60) + ((u-300)*100);

System.out.println("The bill calculated in paisa = " + bill);

/**

Write a program to display the given series.

(i) 2 4 6 8 10....20

(ii) 12 24 36 48 60....n terms

*/

import java.util.*;

class Series1 {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Series (i):");

for (int i = 2; i <= 20; i += 2) {

System.out.print(i + " ");

System.out.print("Enter the number of terms for the second series: ");

int n = sc.nextInt();

System.out.println("Series (ii):");

for (int i = 1; i <= n; i++) {

System.out.print((12 * i) + " ");

}
}

/**

Write a program to enter elements in the square matrix of n x n order and display sum of its
right diagonal elements.

*/

import java.util.*;

class diag {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the order of the square matrix: ");

int m = sc.nextInt();

int[][] matrix = new int[m][m];

System.out.println("Enter the elements of the matrix:");

for (int i = 0; i < m; i++) {

for (int j = 0; j < m; j++) {

matrix[i][j] = sc.nextInt();

int s = 0;

for (int i = 0; i < m; i++) {

s += matrix[i][m - 1 - i];

System.out.println("The entered matrix is:");

/**

Define a class to accept different name and weight in kg for n number of children in a two
different arrays in an ascending order (weight).
Input weight to search whether it is present in the array or not (using Binary Search
Technique).

If it is present then display the corresponding name and if it is not then display an
appropriate message

*/

import java.util.*;

class Weight {

String names[];

double weights[];

void inputDetails(int n) {

Scanner sc = new Scanner(System.in);

names = new String[n];

weights = new double[n];

System.out.println("Enter names and weights of children:");

for (int i = 0; i < n; i++) {

System.out.print("Enter name of child " + (i + 1) + ": ");

names[i…

/**

*/

class Stock {

String item;

double qty, rate, amt;

Stock(String a, double b, double c) {

item=a;

qty=b;

rate=c;
amt=qty*rate;

void display() {

System.out.println("Name of the item: "+item);

System.out.println("Quantity: "+qty);

System.out.println("Rate per unit: "+rate);

System.out.println("Net value "+amt);

class Purchase extends Stock {

int pqty;

double prate;

Purchase(String a, double b, double c, int d, double e) {

super(a, b, c);

pqty=d;

prate=e;

void update() {

qty += pqty;

if(prate != rate) {

rate=prate;

amt = qty*rate;

void …

/**

*
*/

import java.util.*;

class Stock {

String item;

double qty, rate, amt;

Stock(String a, double b, double c) {

item=a;

qty=b;

rate=c;

amt=qty*rate;

void display() {

System.out.println("Name of the item: "+item);

System.out.println("Quantity: "+qty);

System.out.println("Rate per unit: "+rate);

System.out.println("Net value "+amt);

class Purchase extends Stock {

int pqty;

double prate;

Purchase(String a, double b, double c, int d, double e) {

super(a, b, c);

pqty=d;

prate=e;

void update() {
qty += pqty;

if(prate != rate) {

rate=prate;

amt = qty*rate;

/**

WAP to input any number from the user and check and display whether is a Boring Number
or not a Boring Number.

Boring Number: A positive number is called boring if all of the digits of f positions in the
number are even and all For example, the number of the digits at odd positions are old

Input: 1478

Output: 1478 is boring number.

[As the odd positions include the digits (1, 7) which are odd and even positions inchad the
digits 14, 8l which are even.]

*/

import java.util.*;

class Boring {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter any number: ");

int n = sc.nextInt();

int t = n;

int p = 1, c = 0;

while (t != 0) {

int…

import java.util.*;

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

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of array");

int n = sc.nextInt();

int a[] = new int[n];

int c =0;

System.out.println("Enter the elements of array");

for (int i =0; i<n; i++)

a[i] = sc.nextInt();

for (int i = 0; i<n; i++)

int t = a[i];

int nt = 0;

while (t>0) {

int r = t%10;

nt = (nt*10) + r;

t = t/10;

System.out.println("\t" + "Orginal Number " + "\t" + "Reversed Number ");

System.out.println("\t…

import java.util.*;

class Bill {

String name;
int table;

double bill;

public Bill(String name, int tableNumber, double bill) {

this.name = name;

this.table = table;

this.bill = bill;

public void displayBill() {

System.out.println("Customer: " + name);

System.out.println("Table Number: " + table);

System.out.println("Total Bill: Rs." + bill);

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the name of customer");

String s = sc.nextLine();

System.out.println("Enter the table number");

int n = sc.nextInt();

Bill ob = new Bill(s, n, 100.0);

ob.displayBill();

/**

Write a menu-driven program to convert a given temperature from Fahrenheit to Celsius.

For an incorrect choice, an appropriate error message should be displayed.

(use the formulae C5/9 (F-32) and F 1.8C+32)

*/

import java.util.*;
class CelFan {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

double C, F;

System.out.println("Enter choice");

System.out.println("1. Celsius to fahrenheit.");

System.out.println("2. Fahrenheit to Celsius.");

int ch = sc.nextInt();

switch (ch) {

case 1:

System.out.print("Enter temperature in Celsius: ");

C = sc.nextDouble();

F = (C * (9/5)) + 32;

System.out.println("Temp…

/**

Write a program to create the square matrix of order 3 3 and input numbers in it and check
whether the matrix is symmetric or not. A square matrix is said to be symmetric if the
elements of the ith row and jth column are equal to the elements of jth row and ith column,

e.g. a symmetric matrix is given below

123

245

356

*/

import java.util.*;

class Symmetric {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int m[][] = new int [3][3],f = 1,pos = -1;


System.out.println("Enter the elements in the matrix");

for(int i=0;i<3;i++) {

for(int j=0;j<3;j++) {

m[i][j]=sc.nextInt();

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (m[i]…

import java.util.*;

class Railway {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter option for Concession:");

System.out.println("1. Disabled Passenger 2. Patients 3. Senior Citizen 4. Awardee 5.


Others");

int ch = sc.nextInt();

System.out.println("Enter cost of the ticket:");

double tc = sc.nextDouble();

switch (ch) {

case 1:

System.out.println("Ticket Cost with Concession: " + (tc - tc * 0.75));

break;

case 2:

System.out.println("Ticket Cost with Concession: " + (tc - tc * 0.50));

break;

case 3:
System.out.println("Ticket Cos…

/**

Define a class to accept name and marks in Computer applications for 20 students in a two
different arrays.

Arrange the marks in descending order and display the names of first 10 students along with
their marks in descending order. [Bubble Sort] [15]

*/

import java.util.*;

class Marks {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

double m[]=new double[20];

String name[]=new String[20];

System.out.println("Enter 20 names and marks ");

for(int i=0;i<20;i++) {

name[i]=sc.nextLine();

m[i]=sc.nextInt();

for (int i = 0; i < 20 - 1; i++) {

for (int j = 0; j < 20 - i - 1; j++) {

if (m[j] > m[j + 1]) {

double t = m[j];

import java.util.*;

public class series {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of n");

int n = sc.nextInt();
double sum = 0.0;

for (int i = 1; i <= n; i++) {

double term = Math.pow(i, -3); // 1/i^3

if (i % 2 == 0) {

sum -= term;

} else {

sum += term;

System.out.println("The sum of the series for n = " + n + " is: " + sum);

import java.util.*;

public class serie {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of x");

int x = sc.nextInt();

System.out.println("Enter the value of n");

int n = sc.nextInt();

double sum = 1.0;

for (int i = 2; i <= n; i++) {

sum += (Math.pow(x, i) / i);

System.out.println("The sum of the series for x = " + x + " and n = " + n + " is: " + sum);
}

import java.util.*;

class Sales {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int s[][] = new int[5][10];

System.out.println("Enter the sales data:");

for (int i = 0; i < 5; i++) {

System.out.println("Enter sales for Salesman " + (i + 1) + ":");

for (int j = 0; j < 10; j++) {

System.out.print("Product " + (j + 1) + ": ");

s[i][j] = sc.nextInt();

System.out.println("Total sales for each product:");

for (int j = 0; j < 10; j++) {

int p = 0;

for (int i = 0; i < 5; i++) {

p += s[i][j];

System.out.println("Product " + (j + 1) + ": "…

import java.util.*;

class Palindrome {

public static void main(String args[])

{
Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of array");

int n = sc.nextInt();

int a[] = new int[n];

int c =0;

System.out.println("Enter the elements of array");

for (int i =0; i<n; i++)

a[i] = sc.nextInt();

for (int i = 0; i<n; i++)

int t = a[i];

int nt = 0;

while (t>0) {

int r = t%10;

nt = (nt*10) + r;

t = t/10;

System.out.println("\t" + "Orginal Number " + "\t" + "Reversed Number ");

System.out.printl…

import java.util.*;

class Student {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string");
String s = sc.nextLine();

String str = " ";

for (int i = 0; i<s.length(); i++) {

char ch = s.charAt(i);

if ( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' ||


ch == 'I' || ch == 'O' || ch == 'U' )

str = str + ch;

else if (Character.isUpperCase(ch) == true) {

char c = Character.toLowerCase(ch);

str = str + c;

else if (Character.isLowerCase(ch) == true) {

char c = Character.toUpperCase(ch);

import java.util.*;

class Stock1 {

int n;

String itemName[];

int[] qty;

double price[];

public Stock1(int nn) {

n = nn;

itemName = new String[n];

qty = new int[n];

price = new double[n];

}
public void input() {

Scanner sc = new Scanner(System.in);

for (int i = 0; i < n; i++) {

System.out.println("Enter details for item " + (i + 1) + ":");

System.out.print("Item Name: ");

itemName[i] = sc.nextLine();

System.out.print("Quantity: ");

qty[i] = sc.nextInt();

System.out.print("Price: ");

price[i] = sc.nextDouble();

void Buy(String itemNameToSearch, int quantity) {

import java.util.*;

class String {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the first string:");

String s1 = sc.nextLine();

s1 = s1.toUpperCase();

System.out.println("Enter the second string:");

String s2 = sc.nextLine();

s2 = s2.toUpperCase();

String w = "";

for (int i = 0; i < s.length; i++) {

char ch = s1.charAt(i);
char chr = s2.charAt(i);

if ((ch == 'A' || ch == 'E' ||ch == 'I' ||ch == 'O' ||ch == 'U') && (chr == 'A' || chr == 'E'
||chr == 'I' ||chr == 'O' ||chr == 'U') && (i == 0)) {

w = s2.charAt(0) + s1.substring(1, s1.length()-1);

el…

/**

A name is to be said as odd name if the ASCII code of each character become an odd
number.

WAP to accept a name and check whether the given name is odd name or not.

*/

import java.util.*;

class ASCII {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the sentence");

String s = sc.nextLine();

int pos = -1;

for (int i = 0; i < s.length(); i++ ) {

char ch = s.charAt(i);

if ((int) ch % 2 != 0) {

pos = i;

else {

System.out.println("The name '" + s + "' is not an odd name.");

System.exit(0);

if (pos != -1) {
System.out.println("The name '" + s + "' is an odd name.");

/**

WAP to accept a sentence then convert each character to second next character. The
character A becomes C, Y becomes A and Z becomes B.

*/

import java.util.*;

class character {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence: ");

String s = sc.nextLine();

String str = "";

for (int i=0;i<s.length();i++)

int ch = (int) s.charAt(i);

if(ch >= 65 && ch <= 88)

ch+=2;

else if(ch == 89)

ch = 65;

else if(ch == 90)

ch = 66;

System.out.print ((char) ch);

}
}

import java.util.*;

class convert {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the string");

String s = sc.nextLine();

String str = "";

s += " ";

int pos = 0,t;

while (pos < s.length()) {

t = s.indexOf(' ', pos);

String w = s.substring(pos,t);

if (w.compareTo("A") == 0) {

str = str + " " +"AN";

else {

str = str + " " + w;

pos = t + 1;

System.out.println ("The Converted sentence is : "+ str);

// search for substring java in each string

import java.util.*;

class A_javasubstring {

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);

String s[] = new String[5];

System.out.println("Enter 5 strings:");

for (int i = 0; i < 5; i++) {

s[i] = sc.next();

System.out.println("Strings containing 'Java' are found at the following positions:");

boolean found = false;

for (int i = 0; i < 5; i++) {

if (s[i].contains("java")) {

System.out.println("Position " + i + " (String " + (i + 1) + "): " + s[i]);

found = true;

if (!found) {

System.out.println("No strings containing substring 'java' were found.");

import java.util.*;

public class WordsFrequency

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter a sentence:");
String text = sc.nextLine();

String[] keys = text.split(" ");

String[] uniqueKeys;

int count = 0;

System.out.println(text);

uniqueKeys = getUniqueKeys(keys);

for(String key: uniqueKeys)

if(null == key)

break;

for(String s : keys)

if(key.equals(s))

count++;

System.out.println("Count of ["+key+"] i…

/**

1. A library issues books on rental basis at a 2% charge on the cost price of the book per day.
As per the rules of the library, a book can be retained for 7 days without any fine. If the book
is

returned after 7 days, a fine will also be charged for the excess days as per the chart given
below:

Number of excess days Fine per day (Rs.)

1 to 5 2.00
6 to 10 3.00

Above 10 5.00

A super class Library has been defined. Define a sub class Compute to calculate the fine and
the total amount. The details of the members of both the classes are given below:

Class name : Library

Data members/instance variables:

name : to store the name of the book

author : to s…

/**

Define a class to accept names and total marks in 5 subjects of 10 different students in two
different arrays.

Arrange names in alphabetical and display the names of students who scored highest and
lowest total marks. [Using Bubble sort]

*/

import java.util.*;

class SectionB_3 {

public static void main(String args[]) {

String n[] = new String[10];

int m[] = new int[10];

Scanner sc = new Scanner(System.in);

for(int i = 0; i < 10; i++) {

System.out.println("Enter the name of the student " + (i+1));

n[i] = sc.nextLine();

System.out.println("Enter the total marks of the student " + (i+1) + " in 5 subjects");

m[i] = sc.nextInt();

sc.nextLine(); //consume…

// packers

import java.util.*;
class Packers {

Scanner sc = new Scanner(System.in);

int n,w, wt[];

Packers (int num, int w1) {

n = num;

w = w1;

wt = new int[n];

void calculate() {

int tw = 0;

for (int i = 0; i < n ; i ++) {

System.out.println("Enter the number of books in box " + (i+1));

int b = sc.nextInt();

if ( n <= 50) {

tw = (w*b);

wt[i] = tw;

tw = 0;

} else {

System.out.println("Books exceed the limit");

void display() {

int max = 0;

for (int i = 0; i < n-1; i++) {

max = i;

for (int j = i+1; j < n; j++) {


// bro print average of totsl marks of all students and deviation lololol

import java.util.*;

class Marks {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of array");

int N = sc.nextInt();

String name[] = new String[N];

int totalmarks[] = new int[N];

for (int i = 0; i < N; i++) {

System.out.println("Enter the name of student " + (i+1));

name[i] = sc.nextLine();

sc.nextLine(); //consume newline

System.out.println("Enter the marks of student " + (i+1));

totalmarks[i] = sc.nextInt();

int s = 0;

for (int i = 0; i < N; i++) {

s+= totalmarks[i…

// overload function volume()

import java.util.*;

class overloadfunc {

double volume(double r) {

double v = 4/3 * 3.14 * r*r*r;

return v;

double volume (double h, double r) {


double v = 3.14*r*r*h;

return v;

double volume (double l, double h, double b) {

double v = l*h*b;

return v;

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of sphere");

int r = sc.nextInt();

System.out.println("Enter radius of cylinder");

int rad = sc.nextInt();

System.out.println("Enter height of cylinder");

int h = sc.nextInt();

System.out.println("Enter length of cuboid");

// consectuve letters

import java.util.*;

class Consecutives {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string");

String s = sc.next().toUpperCase();

for (int i = 0; i < (s.length()-1); i++) {

char ch = s.charAt(i);

char c = s.charAt(i + 1);


if (c - ch == 1) {

System.out.println("Consecutives = " + ch + " " + c);

import java.util.*;

class tranpose {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of m and n");

int m = sc.nextInt();

int n = sc.nextInt();

int a[][] = new int[m][n];

System.out.println("Enter the elements");

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

a[i][j] = sc.nextInt();

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

System.out.print(a[j][i] + " ");

System.out.println();

}
import java .util.*;

class selec {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

String n[]= new String[10];

int m[] = new int[10];

for (int i = 0; i < 10; i++) {

System.out.println("Enter the name of student " + (i+1));

n[i] = sc.nextLine();

System.out.println("Enter the total marks of student " + (i+1));

m[i] = sc.nextInt();

for (int i = 0; i < 10 - 1; i++) {

int min = i;

for (int j = i+1; j < 10; j++) {

if (n[j].compareTo(n[i]) > 0) {

min = j;

String t = n[min];

n[min] = n[i];

n[i…

// tech number

import java.util.*;

class Tech {

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number");

int n = sc.nextInt();

int t = n, c = 0, s = 0;

while (t > 0) {

int d = t%10;

c++;

t/=10;

if (c%2 != 0) {

System.out.println("Not a tech number");

else {

int ld = n%100;

int fd = n/100;

s = (int) Math.pow((ld + fd),2);

if (n == s) {

System.out.println("A tech number");

else {

System.out.println("Not a tech number");

// overload a class with series of n terms, then fibonacci till 10 terms and sum of series.

import java.util.*;
class Overload {

void series(int n) {

int c = 0, a = 2, s = 0;

while (c < n) {

for (int i = 2; i * i <= a; i++) {

if (a % i == 0) {

c++;

if (c == 2) {

s += a;

a++;

System.out.println("Sum of prime numbers = " + s);

void series() {

int a = 5, b = 10;;

System.out.print(a + " " + b + " ");

for (int i = 1; i <= 10; i++) {

int c = a+b;

System.out.print(c + " ");

a = b;

b = c;

System.out…

// automorphic

import java.util.*;
class Automorphic {

int n;

Automorphic(int n) {

this.n = n;

boolean check(int num) {

n = num;

int t = n, c = 0;

while (t>0) {

int d = t%10;

c++;

t/=10;

int sq = n*n;

int ld = (int) (sq % Math.pow(10,c));

if (n == ld) {

return true;

else {

return false;

void print() {

if (!check(n)) {

System.out.println("Not Automorphic");

else {

System.out.println("Automorphic");
}

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("…

import java.util.*;

class vowel {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string in uppercase: ");

String s1 = sc.nextLine();

int c = 0;

for (int i = 0; i < s1.length(); i++) {

char ch = s1.charAt(i);

if ( ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

c++;

System.out.println("Number of vowels : " + c);

// merge elements of array A and B into C

import java.util.*;

class merge {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of n and m");

int n = sc.nextInt();
int m = sc.nextInt();

int A[] = new int[n];

int B[] = new int[m];

int C[] = new int[n+m];

System.out.println("Enter elements of array A:");

for (int i = 0; i < n; i++) {

A[i] = sc.nextInt();

System.out.println("Enter elements of array B:");

for (int i = 0; i < m; i++) {

B[i] = sc.nextInt();

int a = 0, b = 0, k = 0;

while (a < n && b < m) {

C[k++] = A[a++];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy