Java Solutions
Java Solutions
slip1a & 7a & slip14a: Write a ‘java’ program to display characters from ‘A’ to ‘Z’.
CODE:
public class Slip1A{
public static void main(String[] args){
for(char i='A';i<='Z';i++){
System.out.println(i);
}
}
}
slip1b & slip10b & slip15b: Write a ‘java’ program to copy only non-numeric data from one file to another
file.
CODE:
import java.io.*;
//import java.io.BufferedReader;
//import java.io.FileReader;
//import java.io.IOException;
try{
File infile = new File("C:\\Users\\DELL\\Documents\\java\\old.txt");
File outfile = new File("C:\\Users\\DELL\\Documents\\java\\output.txt");
String str;
byte[] buffer = new byte[1024];
int length;
}
}
slip2a & slip10a: Write a java program to display all the vowels from a given string.
CODE:
public class Slip2A {
public static void main(String[] args) {
String str = "hello world";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
System.out.println("Vowels in string: "+ch);
}
}
}
}
slip2b & slip19b & slip26a & slip29a: Write a Java program to display ASCII values of the characters from
a file.
CODE:
import java.util.*;
import java.io.*;
class Slip26a{
public static void main(String[] agrs) throws IOException{
char ch;
FileReader fr = new FileReader("old.txt");
int c;
while((c = fr.read()) != -1){
ch = (char)c;
if(Character.isDigit(ch) == false && (Character.isSpaceChar(c) == false)){
System.out.println("ASCII " +ch+ ":" +c);
}
}
fr.close();
}
}
slip3a: Write a ‘java’ program to check whether given number is Armstrong or not.
(Use static keyword)
CODE:
public class Slip3A{
public static void main(String[] args) {
int num = 153; // Specified number to check
int temp=num;
int rem,sum=0;
while(num>0){
rem=num%10;
num=num/10;
sum=sum+rem*rem*rem;
}
// Check if the number is an Armstrong number
if (temp==sum) {
System.out.println(" is an Armstrong number.");
} else {
System.out.println(" is not an Armstrong number.");
}
}
}
slip3b: Define an abstract class Shape with abstract methods area () and volume (). Derive
abstract class Shape into two classes Cone and Cylinder. Write a java Program
to calculate area and volume of Cone and Cylinder. (Use Super Keyword.)
CODE:
abstract class Shape {
// Abstract methods for calculating area and volume
abstract double area();
abstract double volume();
}
while(lit.hasPrevious()){
System.out.println(" " +lit.previous());
}
}
}
slip5a: Write a java program to display following pattern:
5
45
345
2345
12345
CODE:
public class NumberPattern {
public static void main(String[] args) {
// Display numbers in the specified pattern
for (int i = 5; i >= 1; i--) {
for (int j = i; j <= 5; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
slip5b & slip24b: Write a java program to accept list of file names through command line. Delete the
files having extension .txt. Display name, location and size of remaining files.
CODE:
import java.io.*;
class Slip5b{
public static void main(String[] args){
for(int i=0; i<args.length; i++){
File file = new File(args[i]);
if(file.isFile()){
String name = file.getName();
if(name.endsWith(".txt")){
file.delete();
System.out.println("file is deleted successfully!" +file);
}
else{
System.out.println("file name: " +name + "\nfile location: " +file.getAbsolutePath() + "\nfile size: "
+file.length() +"bytes");
}
}
else{
System.out.println(args[i] + "is not a file");
}
}
}
}
slip6a: Write a java program to accept a number from user, if it zero then throw user
defined Exception “Number Is Zero”, otherwise calculate the sum of first and last digit
of that number. (Use static keyword).
CODE:
import java.util.Scanner;
// Static method to calculate the sum of the first and last digit
public static int sumOfFirstAndLastDigit(int number) {
int lastDigit = number % 10; // Get last digit
int firstDigit = Character.getNumericValue(Integer.toString(number).charAt(0)); // Get first digit
return firstDigit + lastDigit; // Return the sum
}
try {
// Check if the number is zero
if (num == 0) {
throw new NumberIsZeroException("Number Is Zero");
}
// Calculate and display the sum of the first and last digit
int sum = sumOfFirstAndLastDigit(num);
System.out.println("Sum of the first and last digit: " + sum);
} catch (NumberIsZeroException e) {
System.out.println(e.getMessage()); // Display the exception message
} finally {
// Close the scanner
scanner.close();
}
}
}
slip6b & Slip7b: Write a java program to display transpose of a given matrix.
CODE:
import java.util.Scanner;
interface Shape {
double area();
}
scanner.close();
}
}
Slip8b: Write a java program to display the files having extension .txt from a given
directory.
CODE:
import java.io.*;
class Slip8b{
public static void main(String[] args){
File file = new File("C:\\Users\\DELL\\Documents\\java");
File [] fl = file.listFiles((d, f)-> f.toLowerCase().endsWith(".txt"));
for(File f: fl){
System.out.println(f.getName());
}
}
}
Slip9a & slip16a: Write a java Program to display following pattern:
1
01
010
1010
CODE:
public class PatternDisplay {
public static void main(String[] args) {
int rows = 4; // Number of rows in the pattern
try {
validatePAN(pan);
validateMobile(mobile);
System.out.println("Valid PAN number: " + pan);
System.out.println("Valid Mobile number: " + mobile);
} catch (InvalidDataException e) {
System.out.println(e.getMessage());
} finally {
scanner.close();
}
}
class Slip13a{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("enter no. of elements: ");
int n = sc.nextInt();
ArrayList<String> list = new ArrayList<>();
System.out.println("enter the elements of array list collection: ");
for(int i = 0; i<n; i++){
String ele = sc.next();
list.add(ele);
}
}
}
slip13b & slip17b & slip21b: Write a java program that asks the user name, and then greets the user by
name Before outputting the user's name, convert it to upper case letters. For example, if
the user's name is Raj, then the program should respond "Hello, RAJ, nice to meet
you!".
CODE:
import java.util.Scanner;
scanner.close();
}
}
slip15a & slip24a: Write a java program to search given name into the array, if it is found then display
its index otherwise display appropriate message.
CODE:
import java.util.Scanner;
if (found) {
System.out.println("Name found at index: " + index);
} else {
System.out.println("Name not found in the array.");
}
scanner.close();
}
}
slip16b & slip18b & slip29b: Write a java program to copy the data from one file into another file, while
copying change the case of characters in target file and replaces all digits by ‘*’ symbol.
CODE:
import java.io.*;
class Slip18b{
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("old.txt");
FileWriter fw = new FileWriter("new.txt");
int c;
scanner.close();
}
}
Slip20a: Write a java program using AWT to create a Frame with title “TYBBACA”,
background color RED. If user clicks on close button then frame should close.
CODE:
import java.awt.*;
import java.awt.event.*;
setVisible(true);
}
class Slip21a{
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("old.txt");
FileWriter fw = new FileWriter("new2.txt");
try{
Scanner sc = new Scanner(fr);
while(sc.hasNextLine()){
String s = sc.nextLine();
StringBuffer buffer = new StringBuffer(s);
buffer = buffer.reverse();
String res = buffer.toString();
fw.write(res);
}
}
catch(Exception e){}
fr.close();
fw.close();
}
}
Slip22a: Write a Java program to calculate factorial of a number using recursion.
CODE:
import java.util.Scanner;
class Slip22b{
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
System.out.println("1. create a file \n2. rename a file \n3. delete a file \n4. display file path");
System.out.println("enter a file name: ");
switch(ch){
case 1:
if(file.createNewFile()){
System.out.println("file created: " +file.getName());
}
else{
System.out.println("file already exists");
}
break;
case 2:
System.out.print("enter new file name: ");
String nf = sc.nextLine();
File nf1 = new File(nf);
if(file.renameTo(nf1)){
System.out.println("file renamed");
}
else{
System.out.println("file can't be renamed");
}
break;
case 3:
if(file.delete()){
System.out.println("delete the file: " +file.getName());
}
else{
System.out.println("failed to delete the file");
}
break;
case 4:
System.out.println("file location: " +file.getAbsolutePath());
break;
default:
System.out.println("please choose the correct option");
break;
}
}
}
Slip23a: Write a java program to check whether given file is hidden or not. If not then
display its path, otherwise display appropriate message.
CODE:
slip23a:
import java.io.*;
import java.util.*;
try{
System.out.print("enter file name: ");
String str = sc.nextLine();
File file = new File(str);
if(file.isHidden()){
System.out.println("file is hidden");
}
else{
System.out.println("file location: " +file.getAbsolutePath());
}
}
catch(Exception e){}
}
}
Slip25a: Write a java program to check whether given string is palindrome or not.
CODE:
import java.util.Scanner;
scanner.close();
}
2. CubeSeries.java:
package Series;
3. SquareSeries.java:
package Series;
scanner.close();
}
}
public EmployeeDetails() {
frame = new JFrame("Employee Details");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setLayout(new BorderLayout());
// Panel for input fields
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(4, 2));
inputPanel.add(new JLabel("Salary:"));
salaryField = new JTextField();
inputPanel.add(salaryField);
frame.add(inputPanel, BorderLayout.NORTH);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addEmployee();
}
});
frame.setVisible(true);
}
import java.util.Scanner;
scanner.close();
}