0% found this document useful (0 votes)
66 views47 pages

Course Code: Cse1007 Java Programming SLOT L53+54 Lab Assignment 1 Name: S.Jayashree Registration Number:18Bce2258

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 47

COURSE CODE: CSE1007

JAVA PROGRAMMING

SLOT L53+54

LAB ASSIGNMENT 1

NAME: S.JAYASHREE

REGISTRATION NUMBER:18BCE2258

Programs on Control statements and Arrays

Question 1 : Print the following patterns by finding the table values of stars
and spaces.[Any three patterns only]

Part a:

package jayashreepkg;
import java.util.*;

public class Question1 {


public static void main(String[] args) {
int row;
System.out.println("18BCE2258 Jayashree");
Scanner s = new Scanner(System.in);
row = s.nextInt();
for (int i = 0; i < row; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
Part b:

Code:
package jayashreepkg;
import java.util.*;

public class Question1 {


public static void main(String args[]) {
System.out.println("18BCE2258 Jayashree");
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == 1 || j == n || i == n / 2 && j != n / 2 + 1 || i == n / 2 + 1 || i == n / 2 + 2 && j != n / 2 + 1)
{
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}

}
Output:
Part c:

package jayashreepkg;
import java.util.*;

public class Question1 {


public static void main(String[] args) {
System.out.println("18BCE2258 Jayashree");
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows to print the pattern ");

int rows = scanner.nextInt();

System.out.println("** Printing the pattern... **");

for (int i = rows; i >= 1; i--) {


for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output:
Question 2. Find the HCF and LCM of given numbers.

Code:

package jayashreepkg;

import java.util.*;

public class JavaQuestions {


public static void main(String[] args) {
System.out.println("18BCE2258 Jayashree");

int temp1, temp2, num1, num2, temp, hcf, lcm;


Scanner scanner = new Scanner(System.in);

System.out.print("Enter First Number: ");


num1 = scanner.nextInt();
System.out.print("Enter Second Number: ");
num2 = scanner.nextInt();
scanner.close();

temp1 = num1;
temp2 = num2;

while (temp2 != 0) {
temp = temp2;
temp2 = temp1 % temp2;
temp1 = temp;
}

hcf = temp1;
lcm = (num1 * num2) / hcf;

System.out.println("HCF of input numbers: " + hcf);


System.out.println("LCM of input numbers: " + lcm);
}
}
Output:
Question 3.
Ask the user to enter the marks of a student in the below order.
Maths, M Physics, P Chemistry, C English, E Computer Science, CS
And calculate the metrics according to the below table.
Metric Formula
Overall Average (OA) Sum of all the marks / total number of subjects
Engineering Average (EA) Sum of (M * 2), P and C / 4 Computer Science Average
(CSA) CS Print the output according to the below table
Case Output If OA > 75 and EA > CSA Probable Mech, Civil, EEE, ECE candidate
If OA > 75 and CSA > EA Probable CSE, IT, IS candidate
If OA < 75 and CSA > EA Probable BCA candidate
If OA < 75 and CSA > EA Probable BSc candidate

Code:

package jayashreepkg;

import java.util.*;

import java.util.*;
public class JavaQuestions {
public static void main(String args[])

{
float eng, phy, chem, math, comp;
float total, average;
Scanner op = new Scanner(System.in);
System.out.println("18BCE2258 Jayashree");
System.out.println("Enter marks of five subjects:");
System.out.print("Enter marks of maths subjects:");
math = op.nextFloat();
System.out.print("Enter marks of physics subjects:");
phy = op.nextFloat();
System.out.print("Enter marks of chemistry subjects:");
chem = op.nextFloat();
System.out.print("Enter marks of English subjects:");
eng = op.nextFloat();
System.out.print("Enter marks of computers subjects:");
comp = op.nextFloat();

total = eng + phy + chem + math + comp;


average = (total / 5);
float OA = average;
float EA = math / 2 + phy + chem / 4;
float CSA = comp;
if (OA > 75 && EA > CSA) {
System.out.println("Probable Mech, Civil, EEE, ECE candidate ");
}
if (OA > 75 && CSA > EA) {
System.out.println("Probable CSE, IT, IS candidate ");
}
if (OA < 75 && CSA > EA) {
System.out.println("Probable BCS candidate ");
}
if (OA < 75 && CSA < EA) {
System.out.println("Probable BCA candidate ");
}

}
}

Output:
Question 4. Sort an array of element using bubble sort

Code:

package jayashreepkg;
import java.util.*;

public class JavaQuestions {


static void bubbleSort(int[] arr, int l) {
int n = l;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arr[j - 1] > arr[j]) {
//swap elements
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}

}
}

}
public static void main(String[] args) {
System.out.println("18BCE2258 Jayashree");
int arr[] = new int[100];
Scanner s = new Scanner(System.in);
int l = s.nextInt();

System.out.println("Enter Elements");
for (int i = 0; i < l; i++) {
arr[i] = s.nextInt();
}

bubbleSort(arr, l);

System.out.println("Array After Bubble Sort");


for (int i = 0; i < l; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
Question 5. Remove duplicate elements from a sorted array [only one array should
be used]

Code:

package jayashreepkg;
import java.util.*;

public class JavaQuestions


{ static int removeDuplicates(int arr[], int n)
{
if (n == 0 || n == 1)
return n;

int j = 0;

for (int i = 0; i < n-1; i++)


if (arr[i] != arr[i+1])
arr[j++] = arr[i];

arr[j++] = arr[n-1];

return j;
}

public static void main (String[] args)


{
System.out.println("18BCE2258 Jayashree");
int arr[] = new int[100];
Scanner sc=new Scanner(System.in);

int n = sc.nextInt();
for (int i=0; i<n; i++)
arr[i]=sc.nextInt();

n = removeDuplicates(arr, n);

for (int i=0; i<n; i++)


System.out.print(arr[i]+" ");
}
}
Output:-
Question 6. Check if a given input matrix from a user is an identity matrix

Code:
package jayashreepkg;
import java.util.*;
public class JavaQuestions
{ public static void main(String[] args) {
int rows, cols;
System.out.println("18BCE2258 Jayashree");
Scanner s=new Scanner(System.in);
rows=s.nextInt();
cols=s.nextInt();
boolean flag = true;
int a[][] = new int[100][100];
for(int r=0;r<rows;r++)
{
for(int c=0;c<cols;c++)
{
a[r][c]=s.nextInt();
}
}
if(rows != cols){
System.out.println("Matrix should be a square matrix");
}
else {
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(i == j && a[i][j] != 1){
flag = false;
break;
}
if(i != j && a[i][j] != 0){
flag = false;
break;
}
}
}
if(flag)
System.out.println("Identity matrix");
else
System.out.println("Not an identity matrix");
}
}
}
Output:
Question 7. Display the sum of rows in a matrix

Code:

package jayashreepkg;
import java.util.*;

public class JavaQuestions


{ public static void main(String[] args) {
System.out.println("18BCE2258 Jayashree");
int rows, cols, sumRow, sumCol;
Scanner s=new Scanner(System.in);
rows=s.nextInt();
cols=s.nextInt();
int a[][]= new int[rows][cols];

for(int r=0;r<rows;r++)
{
for(int c=0;c<cols;c++)
{
a[r][c]=s.nextInt();
}
}
for(int i = 0; i < rows; i++){
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
System.out.println("Sum of " + (i+1) +" row: " + sumRow);
}

}
}
Output:-
Question 8. Display the addition result of two matrices

Code:

package jayashreepkg;
import java.util.*;

import java.util.*;
public class JavaQuestions {
public static void main(String args[]) {
System.out.println("18BCE2258 Jayashree");
int row, col, i, j;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows");


row = in .nextInt();

System.out.println("Enter the number columns");


col = in .nextInt();

int mat1[][] = new int[row][col];


int mat2[][] = new int[row][col];
int res[][] = new int[row][col];

System.out.println("Enter the elements of matrix1");

for (i = 0; i < row; i++) {

for (j = 0; j < col; j++)


mat1[i][j] = in .nextInt();

System.out.println();
}
System.out.println("Enter the elements of matrix2");

for (i = 0; i < row; i++) {

for (j = 0; j < col; j++)


mat2[i][j] = in .nextInt();

System.out.println();
}

for (i = 0; i < row; i++)


for (j = 0; j < col; j++)
res[i][j] = mat1[i][j] + mat2[i][j];

System.out.println("Sum of matrices:-");

for (i = 0; i < row; i++) {


for (j = 0; j < col; j++)
System.out.print(res[i][j] + "\t");

System.out.println();
}

Output:
Programs on Strings

Question 1:

Code:

package jayashreepkg;
import java.util.*;

public class JavaQuestions {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("18BCE2258 Jayashree");

int m;
System.out.println("Enter the number of semesters");
m = sc.nextInt();

// Declaring 2-D array with m rows


int arr[][] = new int[m][];

//Initializing the columns for each rows of jagged array


System.out.println("Enter the number of subjects in each semester one by one ");
for (int i = 0; i < m; i++) {
arr[i] = new int[sc.nextInt()];
}

int pass = 0;
int fail = 0;

System.out.println("Enter the marks");


for (int i = 0; i < arr.length; i++) //For Rows
{
for (int j = 0; j < arr[i].length; j++) //For Columns
{
arr[i][j] = sc.nextInt();
if (arr[i][j] >= 40) {
pass++;
} else {
fail++;
}
}
}

System.out.println("Marks in different semesters");


for (int i = 0; i < arr.length; i++) //For Rows
{
for (int j = 0; j < arr[i].length; j++) //For Columns
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}

System.out.println(" Number of subjects passed: " + pass);


System.out.println(" Number of subjects failed: " + fail);

}
}
Output:
Question 2 :

Code:

package jayashreepkg;

import java.io.*;
import java.util.*;

public class JavaQuestions {

static int countOccurrences(String str, String word) {


// split the string by spaces in a
String a[] = str.split(" ");

// search for pattern in a


int count = 0;
for (int i = 0; i < a.length; i++) {
// if match found increase count
if (word.equals(a[i]))
count++;
}

return count;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
System.out.println("S.Jayashree : 18BCE2258");
String word = " VIT ";
int res = countOccurrences(str, word);
if (res == 0) {
System.out.println("No such word in the sentence");
} else {
System.out.println(res);
}
}
}
Output:

Question 3:

Code:

package jayashreepkg;
import java.io.*;
import java.util.*;
public class JavaQuestions{
public static void main(String []args){
Scanner s=new Scanner(System.in);
String[] reg=new String[5];
System.out.println("S.JAYASHREE : 18BCE2258");
String scope="BCE";
int one=0;
String sense="BEC";
int two=0;
int three=0;
for(int i=0;i<5;i++)
{
reg[i]=s.nextLine();
if(reg[i].contains(scope))
{
one++;
}
if(reg[i].contains(sense))
{
two++;
}
else
{
three++;
}

}
if(three==0)
{
System.out.println(" SCOPE: "+one);
System.out.println(" SENSE: "+two);

}
else
{
System.out.println(" There are no students from SCOPE or SENSE school" );
}

}
}
Output :-

Question 4:

CODE:

package jayashreepkg;

import java.io.*;
import java.util.*;
public class JavaQuestions {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String[] st = new String[5];
for (int i = 0; i < 5; i++) {
st[i] = s.nextLine();
}

String tf;
int flag = 0;
tf = s.nextLine();
for (int j = 0; j < 5; j++) {
if (st[j].contains(tf)) {
if (flag == 0) {
System.out.println("Name found");
flag = 1;
}
}
}

if (flag == 0) {
System.out.println("Name not found");
}

}
}

Output :-
Question 5:

CODE:

package jayashreepkg;

import java.io.*;
import java.util.*;
public class JavaQuestions {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String username;
String password;
username = s.nextLine();
password = s.nextLine();
int i = 0;
int flag = 0;
while (i < username.length() - 3) {
if (password.contains(username.substring(i, i + 3))) {
if (flag == 0) {
System.out.println("Weak Password");
flag = 1;
}
}
i = i + 1;
}
if (flag == 0) {
System.out.println("Strong password");
}

}
}
Output:

Question 6:

Code:

package jayashreepkg;

import java.io.*;
import java.util.*;
public class JavaQuestions {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String username;
String password;
String confirm;
System.out.println("Enter username: ");
username = s.nextLine();
System.out.println("Enter password: ");
password = s.nextLine();
System.out.println("Confirm password: ");
confirm = s.nextLine();

if (username.isEmpty() == true || password.isEmpty() == true) {


System.out.println("Enter non-empty values");
}
if (password.length() < 8) {
System.out.println("Password length should me minimum 8");
}
if (!password.equals(confirm)) {
System.out.println("Password entered incorrectly");
}
if (password.contains(username)) {
System.out.println("Password contains username");
}
}
}

Output :-
Question 7:

Code:

package jayashreepkg;

import java.util.*;

public class JavaQuestions {


public void reverseWordInMyString(String str) {
String[] words = str.split(" ");
String reversedString = "";
for (int i = 0; i < words.length; i++) {
String word = words[i];
String reverseWord = "";
for (int j = word.length() - 1; j >= 0; j--) {

reverseWord = reverseWord + word.charAt(j);


}
reversedString = reversedString + reverseWord + " ";
}
System.out.println(str);
System.out.println(reversedString);
}

public static void main(String[] args) {


JavaQuestions obj = new JavaQuestions();
obj.reverseWordInMyString("Hello This is Jayashree");
obj.reverseWordInMyString("This is my lab assignment");
}
}

Output :-

Question 8:

Code:

package jayashreepkg;

import java.util.*;
public class JavaQuestions {
public static void main(String[] args) {
System.out.println("18BCE2258 jayashree: ");
Scanner input = new Scanner(System.in);
String inputString;
System.out.print("Enter String: ");
inputString = input.nextLine();
String result = "";
for (int i = 0; i < inputString.length(); i++) {
result += Integer.toHexString(inputString.charAt(i));
}
System.out.println("18BCE2258 jayashree: " + result);
result = result.replaceAll("0", "0000");
result = result.replaceAll("1", "0001");
result = result.replaceAll("2", "0010");
result = result.replaceAll("3", "0011");
result = result.replaceAll("4", "0100");
result = result.replaceAll("5", "0101");
result = result.replaceAll("6", "0110");
result = result.replaceAll("7", "0111");
result = result.replaceAll("8", "1000");
result = result.replaceAll("9", "1001");
result = result.replaceAll("a", "1010");
result = result.replaceAll("b", "1011");
result = result.replaceAll("c", "1100");
result = result.replaceAll("d", "1101");
result = result.replaceAll("e", "1110");
result = result.replaceAll("f", "1111");
System.out.println("Binary: " + result);
}
}

Output :-
Classes and Objects

Question 1:

Code:

package jayashreepkg;

import java.util.*;

import java.util.Scanner;

class Course {
private String name, ID, type, offeredBy;

public void setCourseDetails(String Name, String ID, String Type, String OfferedBy) {
this.name = Name;
this.ID = ID;
this.type = Type;
this.offeredBy = OfferedBy;
}

public static void displayCourseDetails(Course arr[]) {


int UE = 0;
int PE = 0;
int PC = 0;

for (int i = 0; i < arr.length; i++) {


if (arr[i].type.equals("UE"))
UE++;
else if (arr[i].type.equals("PE"))
PE++;
else if (arr[i].type.equals("PC"))
PC++;
}

System.out.println("Number of UE" + UE + "\nNumber of PE" + PE + "\nNumber of PC" + PC);


}

public static void searchCourseDetails(Course arr[], String name) {


int f = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i].name.equals(name)) {
System.out.println("Name : " + arr[i].name + "\tID : " + arr[i].ID + "\tType : " + arr[i].type +
"\tOffered By : " + arr[i].offeredBy);
}
}

public class JavaQuestions {


public static void main(String[] args) {
System.out.println("18BCE2258 S.Jayashree");
Course c[] = new Course[5];
String name, ID, type, offeredBy;

Scanner sc = new Scanner(System.in);

for (int i = 0; i < 5; i++) {


System.out.println("\nCourse " + (i + 1));
c[i] = new Course();
System.out.print("Enter Name : ");
name = sc.nextLine();
System.out.print("Enter ID : ");
ID = sc.nextLine();
System.out.print("Enter Type(UE/PE/PC) : ");
type = sc.nextLine();
System.out.print("Enter Offered By : ");
offeredBy = sc.nextLine();
c[i].setCourseDetails(name, ID, type, offeredBy);
}

System.out.print("\nEnter Course Name to Search : ");


name = sc.nextLine();
Course.searchCourseDetails(c, name);
System.out.println();
Course.displayCourseDetails(c);

sc.close();
}

}
Output :-
Question 2:

Code:

package jayashreepkg;

import java.util.*;

public class JavaQuestions {

String mobile_name;
String company_name;
double price;
int year;

Scanner sc = new Scanner(System.in);

public void setData(String mn, String cn, double p, int y) {


this.mobile_name = mn;
this.company_name = cn;
this.price = p;
this.year = y;
}

public static void sortMobileDetails(JavaQuestions arr[]) {


Arrays.sort(arr, Comparator.comparing(o -> o.company_name));
}

public static void display(JavaQuestions arr[]) {

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i].mobile_name + " " + arr[i].company_name + " " + arr[i].price + " " +


arr[i].year);
}
}

public static void main(String args[]) {


System.out.println("18BCE2258 S.Jayashree");

Scanner reader = new Scanner(System.in);

System.out.print("Enter no of entries: ");


int n = reader.nextInt();

JavaQuestions arr[] = new JavaQuestions[n];


for (int i = 0; i < n; i++) {
arr[i] = new JavaQuestions();
System.out.println("Enter mobile name: ");
reader.nextLine();
String mn = reader.nextLine();
System.out.println("Enter company name: ");
String cn = reader.nextLine();
System.out.println("Enter price: ");
double p = reader.nextDouble();
System.out.println("Enter year: ");
int y = reader.nextInt();
arr[i].setData(mn, cn, p, y);
}

sortMobileDetails(arr);
display(arr);
}
}
Output :-
Question 3:

Code:

import java.io.*;

abstract class Shape{

abstract public void calc_volume();


final float pi=3.14f;
}

class Sphere extends Shape{


double r;

private double volume;


public void accept() throws IOException{
System.out.println("Enter the radius of the Sphere: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
r=Double.parseDouble(br.readLine());
}

}
public void calc_volume(){

volume=1.33333333334*pi*r*r*r;
}
public void display(){

calc_volume();

System.out.println("The volume of sphere is: "+volume);


}
}

class Cone extends Shape{


double h,r,volume;
public void accept() throws IOException{
System.out.println("Enter radius and height of the Cone: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
r=Double.parseDouble(br.readLine());
h=Double.parseDouble(br.readLine());
}

public void calc_volume(){


double d=h/3;
volume=pi*r*r*d;
}
public void display(){

calc_volume();

System.out.println("The volume of Cone is: "+volume);


}
}

class Cylinder extends Shape{


double r,h,volume;
public void accept() throws IOException{
System.out.println("Enter radius and height of the Cylinder: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
r=Double.parseDouble(br.readLine());
h=Double.parseDouble(br.readLine());
}

public void calc_volume(){


volume=pi*r*r*h;
}
public void display(){

calc_volume();
System.out.println("The volume of Cylinder is: "+volume);
}
}

class cube extends Shape{


double l,volume;
public void accept() throws IOException{
System.out.println("Enter length of the Cube: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
l=Double.parseDouble(br.readLine());
}

public void calc_volume(){


volume=l*l*l;
}
public void display(){
calc_volume();
System.out.println("The volume of Box is: "+volume);
}
}

public class sa2 {


public static void main(String [] args)throws IOException{
System.out.println(“18BCE2258 S.Jayashree”);
Sphere s=new Sphere();
s.accept();
s.display();
Cone co=new Cone();
co.accept();
co.display();
Cylinder cy=new Cylinder();
cy.accept();
cy.display();
cube b=new cube();
b.accept();
b.display();
}
}
Output:

You might also like