CE146_JT_LAB_3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab 3

Topics: String, StringBuffer, StringBuilder, array of objects, this


keyword, constructor overloading
Q1. Write a program that returns the number of times that the string “hi” appears
anywhere in the given string.
Code :
import java.util.Scanner;
public class Lab3_1 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter String : ");
String str = sc.nextLine();
int count = 0;
for(int i = 0; i < str.length()-2; i++) {
if(str.substring(i, i+2).equals("hi")) {
count++;
}
}
System.out.println("Count is : " + count);
}
}

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];

public Student(int roll_no) {


super();
this.roll_no = roll_no;
}

public Student(int roll_no, String name) {


super();
this.roll_no = roll_no;
this.name = name;
}

public Student(int roll_no, String name, int[] marks) {


super();
this.roll_no = roll_no;
this.name = name;
this.marks = marks;
}

double findAverage() {
double sum = 0;
for(int mark : marks) {
sum += mark;
}
return (sum/5);
}
}

public class TestStudent {


public static void main(String[] args) {
Student st = new Student(101, "Parth", new int[] {100, 90, 80, 85, 95});
System.out.println("Roll Number : " + st.roll_no);
System.out.println("Name : " + st.name);
System.out.print("Marks : ");
for(int i = 0; i < st.marks.length; i++) {
System.out.print(st.marks[i] + " ");
}
Student[] students = new Student[3];
students[0] = new Student(102, "Dhyey", new int[]{98, 86, 88, 67, 92});
students[1] = new Student(103, "Ridham", new int[]{85, 90, 76, 80, 88});
students[2] = new Student(104, "Akaash", new int[]{72, 80, 68, 85, 95});

Student topStudent = students[0];


for(int i=1; i < students.length; i++) {
if(students[i].findAverage() > topStudent.findAverage())
{
topStudent = students[i];
}
}
System.out.println("\nStudent Having Highest Average Marks is : ");
System.out.println("Roll Number : " + topStudent.roll_no);
System.out.println("Name : " + topStudent.name);
System.out.print("Marks : ");
for(int i = 0; i < topStudent.marks.length; i++) {
System.out.print(topStudent.marks[i] + " ");
}
}
}

Output :

You might also like