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

Array Programs Class 10TH

The document contains 6 programming problems and their solutions in Java. The problems cover topics like defining classes, income tax calculation, string operations, method overloading, menu driven programs and searching arrays. Complete solutions to each problem are provided using concepts like classes, methods, conditionals, loops, arrays etc.

Uploaded by

Bala Krishnan
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)
61 views

Array Programs Class 10TH

The document contains 6 programming problems and their solutions in Java. The problems cover topics like defining classes, income tax calculation, string operations, method overloading, menu driven programs and searching arrays. Complete solutions to each problem are provided using concepts like classes, methods, conditionals, loops, arrays etc.

Uploaded by

Bala Krishnan
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/ 10

Program 1- Define a class called Library with the following description:

Instance variables/data members:


Int acc_num – stores the accession number of the book
String title – stores the title of the book stores the name of the author
Member Methods:
(i) void input() – To input and store the accession number, title and author.
(ii)void compute – To accept the number of days late, calculate and display and fine
charged at the rate of Rs.2 per day.
(iii) void display() To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member
methods.
Solution -

public class Library {

int acc_num;
String title;
String author;

public void input() throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter accession number: ");
acc_num = Integer.parseInt(br.readLine());
System.out.print("Enter title: ");
title = br.readLine();
System.out.print("Enter author: ");
author = br.readLine();
}

public void compute() throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter number of days late: ");
int daysLate = Integer.parseInt(br.readLine());;
int fine = 2 * daysLate;
System.out.println("Fine is Rs " + fine);
}

public void display() {


System.out.println("Accession Number\tTitle\tAuthor");
System.out.println(acc_num + "\t" + title + "\t" + author);
}

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


Library library = new Library();
library.input();
library.compute();
library.display();
}
}

Program 2- Given below is a hypothetical table showing rates of Income Tax for male
citizens below the age of 65 years:

Taxable Income (TI) in Income Tax in

Does not exceed 1,60,000 Nil

Is greater than 1,60,000 and less than or equal to 5,00,000 ( TI – 1,60,000 ) * 10%

Is greater than 5,00,000 and less than or equal to 8,00,000 [ (TI - 5,00,000 ) *20% ] + 34,000

Is greater than 8,00,000 [ (TI - 8,00,000 ) *30% ] + 94,000

Write a program to input the age, gender (male or female) and Taxable Income of a
person.If the age is more than 65 years or the gender is female, display “wrong
category*.
If the age is less than or equal to 65 years and the gender is male, compute and display
the Income Tax payable as per the table given above.

Solution.
import java.util.Scanner;

public class IncomeTax {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
System.out.print("Enter gender: ");
String gender = scanner.next();
System.out.print("Enter taxable income: ");
int income = scanner.nextInt();
if (age > 65 || gender.equals("female")) {
System.out.println("Wrong category");
} else {
double tax;
if (income <= 160000) { tax =
0; } else if (income > 160000 && income <= 500000)
{ tax = (income - 160000) * 10 /
100; } else if (income >= 500000 && income <= 800000)
{
tax = (income - 500000) * 20 / 100 + 34000;
} else {
tax = (income - 800000) * 30 / 100 + 94000;
}
System.out.println("Income tax is " + tax);
}
}
}

Program 3- Write a program to accept a string. Convert the string to uppercase. Count
and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output: 4

Solution.
import java.util.Scanner;

public class StringOperations {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.nextLine();
input = input.toUpperCase();
int count = 0;
for (int i = 1; i < input.length(); i++) {
if (input.charAt(i) == input.charAt(i - 1)) {
count++;
}
}
System.out.println(count);
}

Program4- W Design a class to overload a function polygon() as follows:


(i) void polygon(int n, char ch) : with one integer argument and one character
type argument that draws a filled square of side n using the character stored
in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a
filled rectangle of length x and breadth y, using the symbol ‘@’
(iii)void polygon( ) : with no argument that draws a filled triangle shown
below.

Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

Solution.

public class Overloading {


public void polygon(int n, char ch) {

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

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

System.out.print(ch);

System.out.println();

public void polygon(int x, int y) {

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

for (int j = 1; j <= y; j++) {

System.out.print("@");

System.out.println();

public void polygon() {

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

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


System.out.print("*");

System.out.println();

Program 5- Using the switch statement, writw a menu driven program to:
(i) Generate and display the first 10 terms of the Fibonacci
series 0,1,1,2,3,5….The first two Fibonacci numbers are 0 and 1, and each
subsequent number is the sum of the previous two.
(ii)Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits=18
For an incorrect choice, an appropriate error message should be displayed

Solution.

import java.util.Scanner;

public class Menu {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.println("Menu");

System.out.println("1. Fibonacci Sequence");

System.out.println("2. Sum of Digits");

System.out.print("Enter choice: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

int a = 0;

int b = 1;

System.out.print("0 1 ");

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


{ int c = a +
b; System.out.print(c + "
"); a = b; b =
c; } break; case
2: System.out.print("Enter a number:
"); int num =
scanner.nextInt(); int sum =
0; while (num > 0) {

int rem = num % 10;

sum = sum + rem;

num = num / 10;

System.out.println("Sum of digits is " + sum);


break;

default:

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

Program6- Write a program to accept the names of 10 cities in a single dimension


string array and their STD (Subscribers Trunk Dialing) codes in another single
dimension integer array. Search for a name of
a city input by the user in the list. If found, display “Search Successful” and print the
name of the city along with its STD code, or else display the message “Search
Unsuccessful, No such city in the list’.

Solution.

import java.util.Scanner;

import java.util.Scanner;

public class Cities {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

String[] cities = new String[10];

int[] std = new int[10];

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

System.out.print("Enter city: ");

cities[i] = scanner.next();

System.out.print("Enter std code: ");

std[i] = scanner.nextInt();

System.out.print("Enter city name to search: ");

String target = scanner.next();

boolean searchSuccessful = false;

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

if (cities[i].equals(target)) {

System.out.println("Search successful");

System.out.println("City : " + cities[i]);

System.out.println("STD code : " + std[i]);

searchSuccessful = true;

break;

}
if (!searchSuccessful) {

System.out.println("Search Unsuccessful, No such city


in the list");

You might also like