CE146_JT_LAB_3
CE146_JT_LAB_3
CE146_JT_LAB_3
Output :
Q2. Write a program which checks whether the input string is palindrome or not and
then display an appropriate message [e.g. “Refer” is a palindrome string].
Code :
import java.util.Scanner;
public class Lab3_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter String : ");
String str = sc.nextLine();
StringBuffer sb = new StringBuffer(str);
if(str.equals(sb.reverse().toString())) {
System.out.println("String is Palindrome");
}
else {
System.out.println("String is not Palindrome");
}
}
Output :
Q3. Write a program that takes your full name as input and displays the abbreviations
of the first and middle names except the last name which is displayed as it is. For
example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
Code :
import java.util.Scanner;
public class Lab3_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter String : ");
String str = sc.nextLine();
String[] tokens = str.split(" ");
for(int i = 0; i < tokens.length-1; i++) {
System.out.print(tokens[i].charAt(0) + ".");
}
System.out.print(tokens[tokens.length-1]);
}
Output :
Q4. Write a method String removeWhiteSpaces(String str) method that removes all the
white spaces from the string passed to the method and returns the modified string. Test
the functionalities using the main() method of the Tester class.
Code :
import java.util.Scanner;
public class Lab3_4 {
String removeWhiteSpaces(String str) {
String[] tokens = str.split(" ");
str = "";
for(String s:tokens) {
str = str + s;
}
return str;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter String : ");
String str = sc.nextLine();
Lab3_4 ob = new Lab3_4();
System.out.println("String without white space : " + ob.removeWhiteSpaces(str));
}
}
Output :
Q5. Write a class Student with member variables int roll_no, String name and an array
to store marks of 5 subjects. Demonstrate constructor overloading and use this
keyword. Write a findAverage() method that returns double value. Write a TestStudent
class containing main() method to do the following:
a) Store the details of one student by creating one object of Student class and display
them.
b) Store the details of 3 students by creating an array of objects of Student class and
display the details of the student who has the highest average amongst the three
students.
Code :
import java.util.Scanner;
class Student {
int roll_no;
String name;
int[] marks = new int[5];
double findAverage() {
double sum = 0;
for(int mark : marks) {
sum += mark;
}
return (sum/5);
}
}
Output :