0% found this document useful (0 votes)
41 views6 pages

Java Methods for Array and String Operations

The document consists of 29 Java programming tasks, each demonstrating different programming concepts such as input/output, methods, classes, and data structures. Tasks include finding the largest and smallest numbers in an array, counting vowels and consonants in a string, checking for prime numbers, and performing matrix operations. Each task is accompanied by sample input and output to illustrate the expected functionality.

Uploaded by

dead46492
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)
41 views6 pages

Java Methods for Array and String Operations

The document consists of 29 Java programming tasks, each demonstrating different programming concepts such as input/output, methods, classes, and data structures. Tasks include finding the largest and smallest numbers in an array, counting vowels and consonants in a string, checking for prime numbers, and performing matrix operations. Each task is accompanied by sample input and output to illustrate the expected functionality.

Uploaded by

dead46492
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

Q1. Input an array of 10 integers and display the largest and smallest number using methods.

import [Link]; public class Program1{ static int max(int[] a){ int m=a[0]; for(int x:a)
if(x>m) m=x; return m; } static int min(int[] a){ int m=a[0]; for(int x:a) if(x
Sample Output:
Input: 3 5 1 9 2 8 7 4 6 0
Output:
Largest: 9
Smallest: 0

Q2. Input a string and count vowels, consonants, digits and spaces using separate methods.
import [Link]; public class Program2{ static int vowels(String s){ int c=0; for(char
ch:[Link]().toCharArray()) if("aeiou".indexOf(ch)>=0) c++; return c; } static int digits(String
s){ int c=0; for(char ch:[Link]()) if([Link](ch)) c++; return c; } static int
spaces(String s){ int c=0; for(char ch:[Link]()) if([Link](ch)) c++; return c; }
static int consonants(String s){ int c=0; for(char ch:[Link]().toCharArray())
if([Link](ch) && "aeiou".indexOf(ch)==-1) c++; return c; } public static void main(String[]
args){ Scanner sc=new Scanner([Link]); String s=[Link](); [Link]("Vowels:
"+vowels(s)); [Link]("Consonants: "+consonants(s)); [Link]("Digits:
"+digits(s)); [Link]("Spaces: "+spaces(s)); } }
Sample Output:
Input: Hello World 123
Output:
Vowels: 3
Consonants: 7
Digits: 3
Spaces: 2

Q3. Create a class Number with a method isPrime(). Input a number and display whether it is prime.
import [Link]; class Number{ int n; Number(int n){ this.n=n; } boolean isPrime(){ if(n<2)
return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; } } public class Program3{
public static void main(String[] args){ Scanner s=new Scanner([Link]); int n=[Link](); Number
num=new Number(n); [Link](n + ([Link]()?" is prime":" is not prime")); } }
Sample Output:
Input: 29
Output:
29 is prime

Q4. Input two arrays of 5 elements each and merge them into a sorted array.
import [Link]; import [Link]; public class Program4{ public static void
main(String[] args){ Scanner s=new Scanner([Link]); int[] a=new int[5], b=new int[5]; for(int
i=0;i<5;i++) a[i]=[Link](); for(int i=0;i<5;i++) b[i]=[Link](); int[] c=new int[10];
[Link](a,0,c,0,5); [Link](b,0,c,5,5); [Link](c);
[Link]([Link](c)); } }
Sample Output:
Input: 1 5 7 10 12
2 6 8 9 11
Output:
[1, 2, 5, 6, 7, 8, 9, 10, 11, 12]

Q5. Define Student with name and marks in 3 subjects. Input and display total and average.
import [Link]; class Student{ String name; int m1,m2,m3; void read(Scanner s){ name=[Link]();
m1=[Link](); m2=[Link](); m3=[Link](); } int total(){ return m1+m2+m3; } double avg(){ return
total()/3.0; } } public class Program5{ public static void main(String[] args){ Scanner s=new
Scanner([Link]); Student st=new Student(); [Link](s); [Link]("Total: "+[Link]());
[Link]("Average: "+[Link]("%.2f",[Link]())); } }
Sample Output:
Input: Rahul 78 85 90
Output:
Total: 253
Average: 84.33

Q6. Overload area() to compute area of circle, rectangle and triangle.


import [Link]; public class Program6{ static double area(double r){ return [Link]*r*r; }
static double area(double l,double b){ return l*b; } static double area(double b,double h, boolean
triangle){ return b*h/2; } public static void main(String[] args){ Scanner s=new Scanner([Link]);
[Link]("Circle r:"); double r=[Link](); [Link]("Rectangle l b:"); double
l=[Link](); double b=[Link](); [Link]("Triangle base height:"); double
bb=[Link](); double h=[Link](); [Link]("Circle area: "+area(r));
[Link]("Rectangle area: "+area(l,b)); [Link]("Triangle area:
"+area(bb,h,true)); } }
Sample Output:
Sample Input: 3 4 5 6 4 5
Sample Output:
Circle area: 28.274333882308138
Rectangle area: 20.0
Triangle area: 10.0

Q7. Series class to generate Fibonacci series up to n terms.


import [Link]; class Series{ static void fib(int n){ int a=0,b=1; for(int i=1;i<=n;i++){
[Link](a+(i==n?"":" ")); int c=a+b; a=b; b=c; } [Link](); } } public class
Program7{ public static void main(String[] args){ Scanner s=new Scanner([Link]); int n=[Link]();
[Link](n); } }
Sample Output:
Input: 7
Output:
0112358

Q8. Search for an element in an array using linear search.


import [Link]; public class Program8{ public static int linear(int[] a,int key){ for(int
i=0;i
Sample Output:
Input: 3 7 5 1 9 5
Output:
Found at index 2

Q9. Input a sentence and display each word on a new line.


import [Link]; public class Program9{ public static void main(String[] args){ Scanner s=new
Scanner([Link]); String line=[Link](); for(String w: [Link]("\\s+")) [Link](w);
} }
Sample Output:
Input: This is Java
Output:
This
is
Java

Q10. Class Box with length, breadth and height. Use constructor to initialize and display volume.
class Box{ int l,b,h; Box(int l,int b,int h){this.l=l;this.b=b;this.h=h;} int volume(){return l*b*h;} }
public class Program10{ public static void main(String[] args){ Box box=new Box(2,3,4);
[Link]("Volume: "+[Link]()); } }
Sample Output:
Output:
Volume: 24
Q11. Input a number and check whether it is an Armstrong number using a method.
import [Link]; public class Program11{ static boolean isArm(int n){ int t=n,s=0; while(t>0){
int d=t%10; s+=d*d*d; t/=10; } return s==n; } public static void main(String[] args){ Scanner s=new
Scanner([Link]); int n=[Link](); [Link](n + (isArm(n)?" is Armstrong":" is not
Armstrong")); } }
Sample Output:
Input: 153
Output:
153 is Armstrong

Q12. Input 10 numbers and display only even numbers using a method.
import [Link]; public class Program12{ static void printEven(int[] a){ for(int x:a)
if(x%2==0) [Link](x+" "); [Link](); } public static void main(String[] args){
Scanner s=new Scanner([Link]); int[] a=new int[10]; for(int i=0;i<10;i++) a[i]=[Link]();
printEven(a); } }
Sample Output:
Input: 1 2 3 4 5 6 7 8 9 10
Output:
2 4 6 8 10

Q13. Class Employee with empid, name and salary. Display updated salary after increment using methods.
class Employee{ int id; String name; double sal; Employee(int id,String name,double
sal){[Link]=id;[Link]=name;[Link]=sal;} void increment(double p){ sal += sal*p/100; } } public
class Program13{ public static void main(String[] args){ Employee e=new Employee(101,"Asha",20000);
[Link](10); [Link]("Updated salary: "+[Link]); } }
Sample Output:
Output:
Updated salary: 22000.0

Q14. Input a string and check whether it is a palindrome.


import [Link]; public class Program14{ static boolean isPal(String s){
s=[Link]("\\s+","").toLowerCase(); return new StringBuilder(s).reverse().toString().equals(s); }
public static void main(String[] args){ Scanner s=new Scanner([Link]); String line=[Link]();
[Link](isPal(line)?"Palindrome":"Not Palindrome"); } }
Sample Output:
Input: Madam
Output:
Palindrome

Q15. Input 10 integers and sort them using selection sort method.
import [Link]; public class Program15{ static void selection(int[] a){ for(int i=0;i
Sample Output:
Input: 5 2 9 1 6 3 8 4 7 0
Output:
0123456789

Q16. Calculator that overloads add() to add two integers, two doubles, and three integers.
public class Program16{ static int add(int a,int b){ return a+b; } static double add(double a,double b){
return a+b; } static int add(int a,int b,int c){ return a+b+c; } public static void main(String[] args){
[Link](add(2,3)); [Link](add(2.5,3.5)); [Link](add(1,2,3)); } }
Sample Output:
Output:
5
6.0
6

Q17. Input a sentence and count the number of words.


import [Link]; public class Program17{ public static void main(String[] args){ Scanner s=new
Scanner([Link]); String line=[Link]().trim(); if([Link]()) [Link](0); else
[Link]([Link]("\\s+").length); } }
Sample Output:
Input: Count words in this sentence
Output:
5

Q18. Input two 3x3 matrices and display their sum.


import [Link]; public class Program18{ public static void main(String[] args){ Scanner s=new
Scanner([Link]); int[][] A=new int[3][3],B=new int[3][3]; for(int i=0;i<3;i++) for(int j=0;j<3;j++)
A[i][j]=[Link](); for(int i=0;i<3;i++) for(int j=0;j<3;j++) B[i][j]=[Link](); for(int
i=0;i<3;i++){ for(int j=0;j<3;j++) [Link]((A[i][j]+B[i][j])+" "); [Link](); } } }
Sample Output:
Input:
123456789
987654321
Output:
10 10 10
10 10 10
10 10 10

Q19. Single inheritance: class A has show(), subclass B displays extended message.
class A{ void show(){ [Link]("From A"); } } class B extends A{ void show(){ [Link]();
[Link]("From B"); } } public class Program19{ public static void main(String[] args){ B
b=new B(); [Link](); } }
Sample Output:
Output:
From A
From B

Q20. Input a string and convert all vowels to uppercase.


import [Link]; public class Program20{ public static void main(String[] args){ Scanner s=new
Scanner([Link]); String str=[Link](); StringBuilder sb=new StringBuilder(); for(char ch:
[Link]()){ if("aeiou".indexOf([Link](ch))>=0)
[Link]([Link](ch)); else [Link](ch); } [Link]([Link]()); } }
Sample Output:
Input: hello world
Output:
hEllO wOrld

Q21. Input 5 names and sort them alphabetically.


import [Link]; import [Link]; public class Program21{ public static void
main(String[] args){ Scanner s=new Scanner([Link]); String[] names=new String[5]; for(int
i=0;i<5;i++) names[i]=[Link](); [Link](names); for(String n:names) [Link](n); } }
Sample Output:
Input: Ravi Aman Sunita Priya Zoya
Output:
Aman
Priya
Ravi
Sunita
Zoya

Q22. Define Time with hours and minutes. Add two Time objects using a method.
class Time{ int h,m; Time(int h,int m){ this.h=h; this.m=m; } Time add(Time t){ int mm=m+t.m; int
hh=h+t.h+mm/60; return new Time(hh, mm%60); } public String toString(){ return h+"h "+m+"m"; } } public
class Program22{ public static void main(String[] args){ Time t1=new Time(1,40), t2=new Time(2,35); Time
t3=[Link](t2); [Link](t3); } }
Sample Output:
Output:
4h 15m

Q23. Input a number and display its factors using a method.


import [Link]; public class Program23{ static void factors(int n){ for(int i=1;i<=n;i++)
if(n%i==0) [Link](i+" "); [Link](); } public static void main(String[] args){
Scanner s=new Scanner([Link]); int n=[Link](); factors(n); } }
Sample Output:
Input: 12
Output:
1 2 3 4 6 12

Q24. Input two strings and check whether they are anagrams.
import [Link]; import [Link]; public class Program24{ static boolean
anagram(String a,String b){ a=[Link]("\\s+","" ).toLowerCase(); b=[Link]("\\s+",""
).toLowerCase(); if([Link]()!=[Link]()) return false; char[] x=[Link](), y=[Link]();
[Link](x); [Link](y); return [Link](x,y); } public static void main(String[]
args){ Scanner s=new Scanner([Link]); String a=[Link](), b=[Link]();
[Link](anagram(a,b)?"Anagram":"Not Anagram"); } }
Sample Output:
Input:
Listen
Silent
Output:
Anagram

Q25. Constructor overloading: default, parameterized, and copy constructor.


class Demo{ int x; Demo(){ x=0; } Demo(int v){ x=v; } Demo(Demo d){ x=d.x; } public String toString(){
return "x="+x; } } public class Program25{ public static void main(String[] args){ Demo d1=new Demo();
Demo d2=new Demo(10); Demo d3=new Demo(d2); [Link](d1+" "+d2+" "+d3); } }
Sample Output:
Output:
x=0 x=10 x=10

Q26. Reverse an array using a method.


import [Link]; public class Program26{ static void reverse(int[] a){ for(int i=0;i
Sample Output:
Input: 1 2 3 4 5
Output:
54321

Q27. Use a method to display multiplication table of a given number.


import [Link]; public class Program27{ static void table(int n){ for(int i=1;i<=10;i++)
[Link](n+" x "+i+" = "+(n*i)); } public static void main(String[] args){ Scanner s=new
Scanner([Link]); table([Link]()); } }
Sample Output:
Input: 7
Output:
7x1=7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70

Q28. Class Person with name and age. Subclass Teen checks if age < 20.
class Person{ String name; int age; Person(String n,int a){name=n;age=a;} } class Teen extends Person{
Teen(String n,int a){ super(n,a); } boolean isTeen(){ return age<20; } } public class Program28{ public
static void main(String[] args){ Teen t=new Teen("Aman",18); [Link]([Link]+" is teen:
"+[Link]()); } }
Sample Output:
Output:
Aman is teen: true

Q29. Input a string and count frequency of each character.


import [Link]; import [Link]; import [Link]; public class Program29{
public static void main(String[] args){ Scanner s=new Scanner([Link]); String line=[Link](); Map
m=new HashMap<>(); for(char ch:[Link]()) [Link](ch, [Link](ch,0)+1); for([Link] e:
[Link]()) [Link]([Link]()+": "+[Link]()); } }
Sample Output:
Input: hello
Output:
h: 1
e: 1
l: 2
o: 1

Q30. Input 10 integers and display the second largest number using a method.
import [Link]; public class Program30{ static int secondLargest(int[] a){ int
first=Integer.MIN_VALUE, second=Integer.MIN_VALUE; for(int x:a){ if(x>first){ second=first; first=x; }
else if(x>second && x!=first) second=x; } return second; } public static void main(String[] args){
Scanner s=new Scanner([Link]); int[] a=new int[10]; for(int i=0;i<10;i++) a[i]=[Link]();
[Link]("Second largest: "+secondLargest(a)); } }
Sample Output:
Input: 5 9 1 7 3 8 2 4 6 0
Output:
Second largest: 8

You might also like