COMSCPBTech498771rObjrPr - Second Module Pgms
COMSCPBTech498771rObjrPr - Second Module Pgms
1. Create two Strings str1 and str2, using String functions find the
a) Character at index 5 for str1
b) Check if the str2 starts with “k”
c) Find the length of str1 and str2
d) Find if str1 and str2 are equal
e) Find the substring of str1 for index 1:4
f) Find the substring of str2 for index 2:6
g) Use getchars function to add characters to a character array dst and display
it( dst[0] and dst[1] have “a”,”b” respectively).
h) Concatenate str2 with presidency University
if(str1.equals(str2))
{
System.out.println("the strings are equal");
}
else
{
System.out.println("Strings are not equal");}
String s2 = str1.substring(1,4);
String s3 = str2.substring(2,6);
System.out.println("The SubString of str1"+s2);
System.out.println("The SubString of str2"+s3);
String s4;
s4 = str1.concat("Presidency University");
System.out.println("The concatination of 2 strings are"+s4);
str1.getChars(0,4,dst,0);
System.out.println(dst);
}
}
2) Create a String “ScienceTechnologyusing StringBuffer, Perform the following
operations
a)insert string “for” at location 7
b) insert 0 at location 2
c) insert “true” at location 3
d) insert a character array
import java.io.*;
public class Stringexample
{
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("ScienceTechnology");
s.insert(7, "for");
System.out.println(s);
s.insert(0, 2);
System.out.println(s);
s.insert(3, "true");
System.out.println(s);
s.insert(2, str_arr);
System.out.println(s);
}
}
3. The company has announced a lucky dip for the employees for Diwali
festival. It is released coupons with unique number printed on it. The criteria
to win the prize are the first and last number should match and there is a
chance of winning only 3 prizes. On the festival day, 5 employees with
different coupon id entered into the final round. Now identify the 3 employees
who will win the lucky dip out of 5.
Palindrome Definition:
If the number is equal to it's reversed number, then the given number is a palindrome
number.
For example, 121 is a palindrome number while 12 is not.
public class PallindromeExample {
public static void main(String[] args) {
if(numbers[i] == reversedNumber)
System.out.println("The employee with coupon
number"+" "+ numbers[i]+" "+ "won the lucky dip");
else
System.out.println("The employee with coupon number"+"
"+numbers[i] +" "+"didn’t won the lucky dip");
}
}
Two Dimensional Array [Matrix Addition]
4. A student visits the university library and in the month of June, July, and
August he read fiction and non-fiction books, and magazines, both in paper
copies and online. You want to keep track of how many different types of
books and magazines he read, and store that information in matrices. Here is
the information below:
June
PAPER ONLINE
Fiction 2 4
Non-fiction 3 1
Magazines 4 5
July
PAPER ONLINE
Fiction 3 2
Non-fiction 1 1
Magazines 5 3
August
PAPER ONLINE
Fiction 1 3
Non-fiction 2 3
Magazines 4 6
class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, i, j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the books which the student read in the month of June and it's
matrix form is");
System.out.println("Enter the books which the student read in the month of July and it's
matrix form is");
System.out.println("Enter the books which the student read in the month of August and it's
matrix form is");
System.out.println();
}
System.out.println("Total 6 paper fiction books the student has read");
System.out.println("Total 9 online fiction books the student has read");
System.out.println("Total 6 paper non-fiction books the student has read");
System.out.println("Total 5 online non- fiction books the student has read");
System.out.println("Total 13 papers the student has read");
System.out.println("Total 14 online magzines the student has read");
}
}
Output:
Enter the number of rows and columns of matrix
3
2
Enter the books which the student read in the month of June and it's matrix form is
2
4
3
1
4
5
Enter the books which the student read in the month of July and it's matrix form is
3
2
1
1
5
3
Enter the books which the student read in the month of August and it's matrix form is
1
3
2
3
4
6
Total number of different types of books and magazines he read is
6 9
6 5
13 14
Total 6 paper fiction books the student has read
Total 9 online fiction books the student has read
Total 6 paper non-fiction books the student has read
Total 5 online non- fiction books the student has read
Total 13 papers the student has read
Total 14 online magzines the student has read
5. Two softball teams submit equipment lists to their sponsors. The equipment
lists for Team A and Team B are as follows:
Team -A 12 45 15
Team -B 15 38 17
Balls 9$
Bats 80$
Gloves 60$
class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
if (n != p)
System.out.println("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("The total cost of the equipment for Team-A and Team-B are:");
System.out.print("\n");
}
}
}
}
Output:
Enter the number of rows and columns of first matrix
2
3
Enter the equipment lists for Team-A
12
45
15
15
38
17
Enter the number of rows and columns of second matrix
3
1
Enter elements of second matrix
9
80
60
The total cost of the equipment for Team-A and Team-B are:
4608
4195
6. Mr. John has joined the CGI Company as Database Administrator and his major
role is to maintain the Employee database which includes Employee Id, Employee
Name, Age and Employee Salary. The criteria for maintaining is that he needs to
store all the records in one single table. How Mr. John is going to access all the
records which he has stored?
Solution:
import java.util.Scanner;
class Employee
{
int Id;
String Name;
int Age;
long Salary;
for(i=0;i<3;i++)
Emp[i] = new Employee(); // Allocating memory to each object
for(i=0;i<3;i++)
{
System.out.print("\nEnter details of "+ (i+1) +" Employee\n");
Emp[i].GetData();
}
System.out.print("\nDetails of Employees\n");
for(i=0;i<3;i++)
Emp[i].PutData();
}
}
Output:
Enter details of 1 Employee
Enter Employee Id : 34
Enter Employee Id : 35
Enter Employee Id : 25
Details of Employees
34 Geetha 23 45000
35 Seetha 24 50000
25 Vineetha28 55000
7) In Presidency University, all the lecturers of CSE has to report to the head
of the department, where HOD is the base class with the data members as
college name and work() method. The sub class is the Lecturers who are
teaching different subjects will extends the properties from the base class.
class HOD{
package javaapplication6;
import java.util.Scanner;
class Area
{
double x=0.0;
double y=0.0;
class JavaApplication6
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1. To Find Area of Square");
System.out.println("Enter 2. To Find Area of Rectangle");
System.out.println("Enter 3. To Find Area of Circle");
int Choice=sc.nextInt();
switch(Choice)
{
case 1: Square a = new Square();
a.calculateArea();
break;
9. The marklist of the students is with the teacher. One of the student has
asked to share her marks. The teacher is worried that if she shares the marks
the student may edit. So help her to share the marks with the students without
letting her to edit
Hint: Final keyword
class Teacher
{
final int marks=75;
void show()
{
System.out.println("Maximum of marks for ech subject is 50");
}
}
class Student extends Teacher
{
void show()
{
Scanner input=new Scanner(System.in);
super.show();
System.out.println("Student:"+marks);
}
}
class finalKey
{
public static void main(String args[])
{
Teacher t=new Teacher();
Teacher s=new Student();
t.show();
s.show();
}
}
class Customerorganisation
{
void address()
{
System.out.println("Rajankunte,Bengaluru");
}
}
class methodOverride
{
public static void main(String args[])
{
customerorganisation o=new customerorganisation();
customerorganisation e=new customerhome();
o.address();
e.address();
}
}