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

Lab Programs

programming for problem solving lab questions
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)
32 views

Lab Programs

programming for problem solving lab questions
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/ 9

LAB PROGRAMS (12+7)

Last date of record submission: 20.09.2024.

Regular Programs:
1. Demonstrate creation of multiple objects of the class.
2. Demonstrate constructor over loading in JAVA.
3. Demonstrate method over loading in Java.
4. Demonstrate this- keyword.
5. Demonstrate type casting in java.
6. Write a Java program that takes an array of integers as input and finds the largest and smallest
elements in the array.
7. Write a Java program that calculates the sum and average of all elements in an array of integers.
8. Write a Java program that reverses the elements of an array. For example, if the input is [1, 2,
3, 4, 5], the output should be [5, 4, 3, 2, 1].
9. Write a Java program that searches for a specific element in an array and prints its position. If
the element is not found, display a message indicating so.
10. Write a Java program that merges two arrays into a single array and displays the combined
array.
11. Write a Java program that finds the second largest element in an array of integers.
12. Write a Java program to find and print the duplicate elements in an array.

Hint: Reading the input:

Common Methods of the Scanner Class

1. Reading Different Data Types:


o nextInt(): Reads an integer.
o nextDouble(): Reads a double.
o nextFloat(): Reads a float.
o nextLong(): Reads a long.
o nextShort(): Reads a short.
o nextByte(): Reads a byte.
o nextBoolean(): Reads a boolean.
2. Reading Strings:
o next(): Reads the next token (word) from the input as a string (stops at
whitespace).
o nextLine(): Reads the entire line of input, including spaces.
3. Checking for Input Availability:
o hasNext(): Returns true if there is another token in the input.
o hasNextInt(): Returns true if the next token is an integer.
o hasNextDouble(): Returns true if the next token is a double.
o hasNextBoolean(): Returns true if the next token is a boolean.
o hasNextLine(): Returns true if there is another line of input.

Usage:

java.util.Scanner sc=new java.util.Scanner(System.in);

n=sc.nextInt();

Sample Program using Scanner class:

import java.util.Scanner;

public class ScannerExample {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Reading an integer

System.out.print("Enter an integer: ");

int intValue = sc.nextInt();

// Reading a double

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

double doubleValue = sc.nextDouble();

// Reading a word (token)

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

String word = sc.next();

// Reading a full line

sc.nextLine(); // Consuming the leftover newline

System.out.print("Enter a full line: ");


String line = sc.nextLine();

// Reading a boolean

System.out.print("Enter true or false: ");

boolean boolValue = sc.nextBoolean();

// Displaying the inputs

System.out.println("Integer: " + intValue);

System.out.println("Double: " + doubleValue);

System.out.println("Word: " + word);

System.out.println("Line: " + line);

System.out.println("Boolean: " + boolValue);

// Closing the scanner

sc.close();

}
Advanced Programs:
1. Program1

Implement a Class called Circle with two instance variable radius of type

integer , area of type of double and three instance methods namely setRadius(),

getRadius(), caluculateArea().

SetRadius() method will set the value for the variable radius ,

getRadius() method will return the value of the radius,

caluculateArea() calculates the area of the circle and prints the area.

Note: the radius of the circle should be a positive integer. if any other radius is provided print -1.

case = 1

input = 2

output = 12.56

case = 2

input = 3

output = 28.26

2. Program 2

Create a class called Employee with instance variables namely eno, ename, basicsal ,da, hra

and create one method namely calculateTotalSal().

calculateTotalSal() should calculate the total salary of the employee based on basicsal &

print the total salary.

Create an executable class called Test in which create an instance of Employee class ,

initialize the members of Employee class with the help of Constructors &

invoke the calculateTotalSal().


Note: da is 10% of basicsal & hra is 15% of basicsal

Read the input in the following manner :- First Eno, followed by Ename followed by basicsal

Sample test case 1

Input

101

Abc

10000

Output

12500

3. Program 3

Write a program to check the given two strings are anagrams or not ? if they are anagrams print 1 else
print -1.

An anagram is a word or phrase that's formed by rearranging the letters of another word or phrase.

case = 1

input =

god

dog

output =

case = 2

input =

124

411

ouput =
-1

4. Program 4

Write a program to print the following pattern for any given postive integer 'n'

input = 4

output =

1234

2341

3412

4123

input = 5

output =

12345

23451

34512

45123

51234

input = 2

output =

12

21

input = 3

output =

123
231

312

5. Program 5

Count common prime factors of two numbers

Given two integer A and B, find the count of common factors of two numbers where factors are prime.

Examples:

input = 6,12

output = 2

Explanation :-

Factors of 6 are 1,2,3

Factors of 12 are 1,2,3,6

Among them 2 and 3 are the only common prime divisors of 6 and 12 whose count is 2

input = 4,8

output = 1

Factors of 4 are 1,2

Factors of 8 are 1,2,4

6. Program 6

Among them 2 is the only commin prime divisor of 4 and 8 whose count is 1

write a java program to read a value n from the user and print the following pattern

input = 5

output =
54321

4321

321

21

input = 6

output =

654321

54321

4321

321

21

1. Write a program to find the sum of prime digits of a given number

input = 245

output = 7

prime digits of 245 are 2 and 5

sum of 2 and 5 is 7

Note :- If no primes are present print '0'

input = 237

output = 12

input = 44

output = 0
7. Program 7

Write a program to check if two numbers are anagrams or not

input = 123

321

output = ANAGRAM

input = 121

113

output = NOT ANAGRAM

If Anagram Number print ANAGRAM else print NOT ANAGRAM

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