Course Code: Cse1007 Java Programming SLOT L53+54 Lab Assignment 1 Name: S.Jayashree Registration Number:18Bce2258
Course Code: Cse1007 Java Programming SLOT L53+54 Lab Assignment 1 Name: S.Jayashree Registration Number:18Bce2258
Course Code: Cse1007 Java Programming SLOT L53+54 Lab Assignment 1 Name: S.Jayashree Registration Number:18Bce2258
JAVA PROGRAMMING
SLOT L53+54
LAB ASSIGNMENT 1
NAME: S.JAYASHREE
REGISTRATION NUMBER:18BCE2258
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.*;
Code:
package jayashreepkg;
import java.util.*;
}
Output:
Part c:
package jayashreepkg;
import java.util.*;
Code:
package jayashreepkg;
import java.util.*;
temp1 = num1;
temp2 = num2;
while (temp2 != 0) {
temp = temp2;
temp2 = temp1 % temp2;
temp1 = temp;
}
hcf = temp1;
lcm = (num1 * num2) / hcf;
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();
}
}
Output:
Question 4. Sort an array of element using bubble sort
Code:
package jayashreepkg;
import java.util.*;
}
}
}
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);
Code:
package jayashreepkg;
import java.util.*;
int j = 0;
arr[j++] = arr[n-1];
return j;
}
int n = sc.nextInt();
for (int i=0; i<n; i++)
arr[i]=sc.nextInt();
n = removeDuplicates(arr, n);
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.*;
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();
}
System.out.println("Enter the elements of matrix2");
System.out.println();
}
System.out.println("Sum of matrices:-");
System.out.println();
}
Output:
Programs on Strings
Question 1:
Code:
package jayashreepkg;
import java.util.*;
int m;
System.out.println("Enter the number of semesters");
m = sc.nextInt();
int pass = 0;
int fail = 0;
}
}
Output:
Question 2 :
Code:
package jayashreepkg;
import java.io.*;
import java.util.*;
return count;
}
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 {
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 {
}
}
Output:
Question 6:
Code:
package jayashreepkg;
import java.io.*;
import java.util.*;
public class JavaQuestions {
Output :-
Question 7:
Code:
package jayashreepkg;
import java.util.*;
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;
}
sc.close();
}
}
Output :-
Question 2:
Code:
package jayashreepkg;
import java.util.*;
String mobile_name;
String company_name;
double price;
int year;
sortMobileDetails(arr);
display(arr);
}
}
Output :-
Question 3:
Code:
import java.io.*;
}
public void calc_volume(){
volume=1.33333333334*pi*r*r*r;
}
public void display(){
calc_volume();
calc_volume();
calc_volume();
System.out.println("The volume of Cylinder is: "+volume);
}
}