100% found this document useful (2 votes)
338 views26 pages

Java Programming

The document contains 8 Java programming assignments. Assignment 1 contains 7 programs that perform tasks like calculating the area of a circle, checking if a number is divisible by 3 and 5, and displaying a subject name based on a room number. Assignment 2 contains 5 programs that sort an array, separate even and odd numbers into different arrays, remove duplicate elements from an array, check if an array contains two elements summing to a number, and calculate the row-wise sum of a 2D array.

Uploaded by

supriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (2 votes)
338 views26 pages

Java Programming

The document contains 8 Java programming assignments. Assignment 1 contains 7 programs that perform tasks like calculating the area of a circle, checking if a number is divisible by 3 and 5, and displaying a subject name based on a room number. Assignment 2 contains 5 programs that sort an array, separate even and odd numbers into different arrays, remove duplicate elements from an array, check if an array contains two elements summing to a number, and calculate the row-wise sum of a 2D array.

Uploaded by

supriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 26

JAVA PROGRAMMING

ASSIGNMENT-1
SUPRIYA. N
18BCE2178
LAB-1
1.Read the radius and print the area of a circle

import java.util.*;
public class Circle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double r;
System.out.println("Enter the radius");
r=sc.nextDouble();
double area=3.14*r*r;
System.out.println("The area of the circle"+area);
}
}
2. Read the number and check whether it is divisible by 3 and 5.

import java.util.*;
public class Divisble
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number");
int a=sc.nextInt();
if(a%3==0&&a%5==0)
System.out.println("The number is divisble by both");
else
System.out.println("The number is not divisble by both");
}
}

3. Display Subject Name based on room number. If the user enters 604 then
display Java Programming, If the user enters 605 then display Python
programming for any other input display Invalid input to the user.

import java.util.*;
public class Subject
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a room number");
int a= sc.nextInt();
if(a==604)
System.out.println("Subject Name: Java Programming");
else if(a==605)
System.out.println("Subject Name: Python Programming");
else
System.out.println("Invalid Number");
}
}

4. Print the sum of first n numbers. If n is 3 then print the sum of 1+2+3 to the
user. Get n from the user.

import java.util.*;
public class Sum
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++)
{
sum = sum+i;
}
System.out.println("The sum is"+sum);
}
}

5. Print the sum of the series 1 2 +2 2 +3 2 up to n terms.

import java.util.*;
public class Series
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++)
{
sum = sum+(i*i);
}
System.out.println("The sum is"+sum);
}
}

6. Print the multiplication table by getting the n from the user.

import java.util.*;
public class Multiplication
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum=0;
System.out.println("The Multiplication table is: ");
for(int i=1;i<=12;i++)
{
sum= n*i;
System.out.println(sum);
}
}
}
7. Provide the option of adding two numbers to the user until the user wants
to exit.

import java.util.*;
public class Option
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
while(true)
{
System.out.println("Enter two numbers ");
int a=s.nextInt();
int b=s.nextInt();
int sum=0;
sum=a+b;
System.out.println("The sum is "+sum);
System.out.println("Do you want to continue?");
System.out.println("(Type 1 to continue or 0 to stop) ");
int i=s.nextInt();
if(i==1)
continue;
if(i==0)
break;
}
}
}

8. Print this pattern for n lines

(a)
import java.util.*;
public class Pattern
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
(b)

import java.util.*;
public class Pattern2
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter a number");
int n=s.nextInt();
for(int i=n;i>0;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
(c)

import java.util.Scanner;
public class Pattern3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++)
{
int x=1;
for (int j=0; j<=i; j++)
{
System.out.print(x+ " ");
x++;
}
System.out.println("");
}
for (int i=rows-1; i>=0; i--)
{
int y=1;
for(int j=0; j <=i-1;j++)
{
System.out.print(y+ " ");
y++;
}
System.out.println("");
}
}
}
LAB-2

1.
import java.util.*;
public class sort
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int a[]={2,48,32,5,6,69,22,14,17,25};
int t;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length-i-1;j++)
{
if(a[j]<a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
System.out.println("The sorted array is");
for(int k=0;k<a.length;k++)
{
System.out.print(a[k]+" ");

}
}
}
2.

import java.util.*;
class oddeven
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int a[] = {5, 29, 1, 36, 15, 78, 2, 51};
int n=0, n1=0;
System.out.println("The given array is: ");
for (int i =0; i<a.length; i++)
{
System.out.print(a[i]+ " ");
if(a[i]%2==0)
n++;
else
n1++;
}
System.out.println(" ");
int b[] = new int[n];
int c[] = new int[n1];
int b1=0,c1=0;
for (int j=0;j<a.length;j++)
{
if(a[j]%2==0)
{
b[b1]=a[j];
b1++;
}
else
{
c[c1]=a[j];
c1++;
}
}
System.out.println("The even odd array is: ");
for (int k=0;k<n;k++)
System.out.print(b[k] + " ");
for (int x=0;x<n1;x++)
System.out.print(c[x] + " ");
}
}
3.

public class duplicate{


public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}

public static void main (String[] args) {


int arr[] = {1,3,5,2,4,5,5,6,3,2};
System.out.println("The given array is: ");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
int length = arr.length;
length = removeDuplicateElements(arr, length);
System.out.println("The length is "+length);
}
}
4.

import java.util.*;
class sum {
static boolean hasArrayTwoCandidates(int A[],int arr_size, int sum)
{
int l, r;
Arrays.sort(A);

l = 0;
r = arr_size - 1;
while (l < r) {
if (A[l] + A[r] == sum)
return true;
else if (A[l] + A[r] < sum)
l++;
else
r--;
}
return false;
}

public static void main(String args[])


{
int A[] = { 1, 4, 45, 6, 10, -8 };
int n = 16;
int arr_size = A.length;

if (hasArrayTwoCandidates(A, arr_size, n))


System.out.println("Array has two "+ "elements with given
sum");
else
System.out.println("Array doesn't have "+ "two elements with
given sum");
}
}
}
5.

import java.io.*;

class sum_row {
static int m = 4;
static int n = 4;

static void row_sum(int arr[][])


{
int i,j,sum = 0;
System.out.print( "\nFinding Sum of each row:\n\n");

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


for (j = 0; j < 4; ++j) {

sum = sum + arr[i][j];


}
System.out.println( "Sum of the row "+ i + " = " + sum);
sum = 0;
}
}
public static void main (String[] args) {
int i,j;
int [][]arr = new int[m][n];

int x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = x++;

row_sum(arr);
}
}

6.
public class matrix_add
{
public static void main(String args[])
{

int a[][]={{2,4,6},{1,5,5},{8,4,9}};
int b[][]={{0,4,2},{6,5,3},{3,2,4}};

int c[][]=new int[3][3];

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

7.
import java.util.*;
public class transpose
{
public static void main(String args[])
{
int i, j;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}

8.

import java.util.*;
public class q8 {
public static void main(String[] args)
{
int i, j;
double t;
int arr[][] = new int[4][];
Scanner obj = new Scanner(System.in);
for(i = 0; i < arr.length; i++)
{
System.out.print("Enter number of students for batch " + (i+1) + ": ");
t = obj.nextDouble();
arr[i] = new int[(int)Math.ceil(t/4)];
for(j = 0; j < arr[i].length; j++)
{
if(t >= 4)
arr[i][j] = 4;
else
arr[i][j] = (int)t;
t = t - 4;
}
}
obj.close();
int cfour = 0;
System.out.println("Contents of 2D Jagged Array");
for (i = 0; i < arr.length; i++)
{
for (j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
if(arr[i][j] == 4)
cfour++;
}
System.out.println();
}
System.out.println("Number of tutors with 4 students are: " + cfour );
}
}
LAB- 3

1.
public class Rectangle
{
double width,height;
Rectangle()
{
width=1;
height=1;
}
Rectangle(double w,double h)
{
width=w;
height=h;
}
double getarea()
{
double a=width*height;
return a;
}
double getPerimeter()
{
double p=2*(width+height);
return p;
}
public static void main(String args[])
{
Rectangle r=new Rectangle(5,50);
Rectangle r1=new Rectangle(2.5,45.7);
double a1=r.getarea();
double p1=r.getPerimeter();
double a2=r1.getarea();
double p2=r1.getPerimeter();
System.out.println("The area of the 1st rectangle is "+a1+"and the perimeter
is "+p1);
System.out.println("The area of the 1st rectangle is "+a2+"and the perimeter
is "+p2);
}
}

2.
import java.util.*;
public class Student
{
String regno,name,course;
double cgpa;
Student(String reg,String n,String c,double gpa)
{
regno=reg;
name=n;
course=c;
cgpa=gpa;
}
public static void main(String args[])
{
Student s[]= new Student[10];
s[0]=new Student("18BCE0994","HRITHIK","BTECH",9.4);
s[1]=new Student("18BCE0991","DISHANK","BTECH",8.85);
s[2]=new Student("18BCE2162","LAYA","BTECH",9.3);
s[3]=new Student("18BCE2040","NIMISHA","BTECH",8.37);
s[4]=new Student("18BCE2040","JAI","BTECH",8.95);
s[5]=new Student("18BCE0435","ZIYAN","BTECH",9.0);
s[6]=new Student("18BCE2177","HARSH","BTECH",9.1);
s[7]=new Student("18BCE2175","SUPRIYA","BTECH",8.1);
s[8]=new Student("18BCE2163","PRIYA","BTECH",8.45);
s[9]=new Student("18BCE2155","AMAAN","BTECH",8.7);
System.out.println("The 9 pointers are: ");
for(int i=0;i<9;i++)
{
if(s[i].cgpa>=9.0)
System.out.println(s[i].regno+” "+s[i].name+" "+s[i].course+"
"+s[i].cgpa);
}
}
}

3.
import java.text.*;
import java.util.*;

public class Main {


public static void main(String[] args) {
Date dt = new Date(1000000000000L);
DateFormat[] dtformat = new DateFormat[6];

dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);

for(DateFormat dateform : dtformat) System.out.println(dateform.format(dt));


}
}

You might also like