[go: up one dir, main page]

0% found this document useful (0 votes)
15 views7 pages

Bob

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

Bob

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

12

import java.util.Scanner;

public class KboatTenthChar


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

Scanner in = new Scanner(System.in);


System.out.print("Enter a character: ");
char ch = in.next().charAt(0);
char nextCh = (char)(ch + 10);
System.out.println("Tenth character from "
+ ch + " is " + nextCh);
}
}
outp:

VDT:

13.Write a program in Java to generate the following pattern:


A
AB
ABC
ABCD
ABCDE

Ans:

OUTPUT:
VDT:

14.Write a program in Java to generate the following pattern:


A
BB
CCC
DDDD
EEEEE

ANS:
class pattern
{
public static void main(String[] args)
{
int n = 4;
for(int i = 0 ; i <= n ; i++)
{
for(int j = 0 ; j <= i ; j++)
{
System.out.print(" "+(char)(65 + i));
}
System.out.println("");
}
}
}

OUTPUT:
VDT:

15.Define a class ElectricityBill with the following specifications:


Class name : ElectricityBill
Data members/Instance variables:
String n : stores the name of the customer
int units : stores the number of units consumed
double bill : stores the amount to be paid
Member methods:
void accept () : to input the name of the customer and number of units consumed
void calculate () : to calculate the bill as per the following tariff:
Number of units Rate per unit
First 100 units Rs.2.00
Next 200 units Rs.3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.

void print( ) — To print the details as follows:


Name of the customer: ………………………
Number of units consumed: ………………………
Bill amount: ………………………

Write a main method to create an object of the class and call the above member
methods.

ANS:
import java.util.Scanner;

public class ElectricBill


{
private String n;
private int units;
private double bill;

public void accept()


{
Scanner in = new Scanner(System.in);
System.out.print("Enter customer name: ");
n = in.nextLine();
System.out.print("Enter units consumed: ");
units = in.nextInt();
}

public void calculate()


{
if (units <= 100)
bill = units * 2;
else if (units <= 300)
bill = 200 + (units - 100) * 3;
else
{
double amt = 200 + 600 + (units - 300) * 5;
double surcharge = (amt * 2.5) / 100.0;
bill = amt + surcharge;
}
}

public void print()


{
System.out.println("Name of the customer\t\t: " + n);
System.out.println("Number of units consumed\t: " + units);
System.out.println("Bill amount\t\t\t: " + bill);
}

public static void main(String args[])


{
ElectricBill obj = new ElectricBill();
obj.accept();
obj.calculate();
obj.print();
}
}

OUTPUT:
VDT:

16.
Define a class Salary with the following specifications:
Class name : Salary
Data members : Name, Address, Phone, Subject, Specialization, Monthly Salary,
income tax.
Member methods:
(i) to accept the details of a teacher including the monthly salary
(ii) to compute the annual income tax at 5% of the annual salary exceeding₹
1,75,000.
(iii) to display the details of the teacher.
Write a main method to create object of the class and call the above member
methods.
[ICSE 2007].

ANS:
class Salary
{

String Name;
String Address;
int Phone;
String SubjectSpecialization;
float MonthlySalary;
float IncomeTax;

// Constructor to initialize the details


public void details(String a, String b, int N, String c, float d)
{
Name = a;
Address = b;
Phone = N;
SubjectSpecialization = c;
MonthlySalary = d;
}

// Method to display the details


public void show()
{
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Phone: " + Phone);
System.out.println("Subject Specialization: " + SubjectSpecialization);
System.out.println("Monthly Salary: " + MonthlySalary);
}
// Method to calculate income tax
public void Income_Tax()
{
float AnnualSalary = 12 * MonthlySalary;
if (AnnualSalary > 175000)
{
IncomeTax = (0.05f) * (AnnualSalary - 175000); // Calculate tax on the
excess amount
System.out.println("Income Tax: " + IncomeTax);
} else
{
System.out.println("Income Tax: 0.0 (No tax)");
}
}

// Main method to test the functionality


public static void main(String[] ar)
{
Salary p = new Salary();
p.details("Tabassum", "Pune", 973573637, "Computers", 32000.0f);
p.show();
p.Income_Tax();
}
}

OUTPUT:
VDT:

17.Write a program to accept a String and display:


i. The number of lowercase characters
ii. The number of uppercase characters
iii. The number of special characters
iv. The number of digits present in the string
Sample Input: S.T.D code of New Delhi - 011
Sample Output:
The number of lowercase characters = 12
The number of uppercase characters = 5
The number of special characters = 9
The number of digits present in the string = 3 [ICSE 2002]

ANS:
import java.util.Scanner;

public class computer


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

int len = str.length();

int uc =0;
int lc =0;
int sc =0;
int dc =0;
char ch;
for (int i = 0; i < len; i++)
{
ch = str.charAt(i);
if (Character.isUpperCase(ch))
uc++;
else if (Character.isLowerCase(ch))
lc++;
else if (Character.isDigit(ch))
dc++;
else if (!Character.isWhitespace(ch))
sc++;
}

System.out.println("UpperCase Count = " + uc);


System.out.println("LowerCase Count = " + lc);
System.out.println("Digit count = " + dc);
System.out.println("Special Character Count = " + sc);

}
}
OUTPUT:
VDT:

18.Write a program to accept a name and display the initials along with the
surname.
Sample Input: Mohandas Karamchand Gandhi
Sample Output: M. K. Gandhi.

ANS:
import java.util.Scanner;

public class KboatSurnameFirst


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a name of 3 words:");
String name = in.nextLine();

/*
* Get the last index
* of space in the string
*/
int lastSpaceIdx = name.lastIndexOf(' ');

String surname = name.substring(lastSpaceIdx + 1);


String initialName = name.substring(0, lastSpaceIdx);

System.out.println(surname + " " + initialName);


}
}

OUTPUT:
VDT:

19.A string is said to be Unique, if none of the letters present in the string are
repeated. Write a program to accept a string and check whether the string is Unique
or not. The program displays a message accordingly.
Sample Input: COMPUTER
Sample Output: Unique String.
ANS:
import java.util.Scanner;

public class UniqueString


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.nextLine();
str = str.toUpperCase();
boolean isUnique = true;
int len = str.length();

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


{

char ch = str.charAt(i);

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


{
if (ch == str.charAt(j))
{
isUnique = false;
break;
}
}

if (!isUnique)
break;
}

if (isUnique)
System.out.println("Unique String");
else
System.out.println("Not Unique String");
}
}

OUTPUT:
VDT:

20.Write a program to store names of 20 students and their telephone


numbers correspondingly in two single dimensional arrays. Enter a name
separately and search it in the given list of names. If it is present then
display the name with its telephone number otherwise, display a message
"Name not found". Use linear search technique.

ANS:
import java.util.Scanner;

public class StudentPhoneBook {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Initialize arrays to store student names and telephone numbers


String[] names = new String[20];
String[] phoneNumbers = new String[20];

// Input student names and their phone numbers


System.out.println("Enter the names and phone numbers of 20 students:");
for (int i = 0; i < 20; i++) {
System.out.print("Enter name of student " + (i + 1) + ": ");
names[i] = scanner.nextLine();
System.out.print("Enter phone number of student " + (i + 1) + ": ");
phoneNumbers[i] = scanner.nextLine();
}

// Asking user to enter the name to search


System.out.print("\nEnter the name to search: ");
String searchName = scanner.nextLine();

// Perform linear search


boolean found = false;
for (int i = 0; i < 20; i++) {
if (names[i].equalsIgnoreCase(searchName)) {
System.out.println("Name: " + names[i] + ", Phone Number: " +
phoneNumbers[i]);
found = true;
break;
}
}

if (!found) {
System.out.println("Name not found.");
}

scanner.close();
}
}

OUTPUT:

VDT:

You might also like