Java Record
Java Record
1) a) Implement the following programs using command line arguments and Scanner class
i) Accept two strings from the user and print it on console with concatenation of “and” in the
middle of the strings.
Code:
class Onea
Output:
D:\Java>java Onea
Java programming
}
OUTPUT :
D:\Java>java Onea2
Java
Programming
ii) To find the perimeter and area of a circle given a value of radius.
class Onea3
double r = Double.parseDouble(args[0]);
Output:
D:\Java>java Onea3
import java.util.Scanner;
class Oneb
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double radius;
System.out.println("Enter the radius : ");
radius = sc.nextDouble();
System.out.println("Area of circle : " + radius*radius);
System.out.println("Perimeter of circle : " + 2*3.14*radius);
}
}
Output:
D:\Java>java Oneb
Enter the radius :
7
Area of circle : 49.0
Perimeter of circle : 43.96
b) Write a program using classes and objects in java?
Code:
import java.util.Scanner;
class cal
{
void add(int x, int y)
{
System.out.println("The sum of two numbers : " + (x+y));
}
void sub(int x, int y){
System.out.println("The difference of two numbers : " + (x-y));
}
void mul(int x, int y){
System.out.println("The product of two numbers : " + x*y);
}
void div(int x, int y){
System.out.println("The quotient of x/y : " + x/y);
}
void modDiv(int x, int y){
System.out.println("The remainder when x%y : " + x%y);
}
}
class Onec
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int x,y;
System.out.println("Enter the two numbers x and y : ");
x = scan.nextInt();
y = scan.nextInt();
cal obj = new cal();
obj.add(x,y);
obj.sub(x,y);
obj.mul(x,y);
obj.div(x,y);
obj.modDiv(x,y);
}
}
Output:
D:\Java>java Onec
Enter the two numbers x and y :
2
3
The sum of two numbers : 5
The difference of two numbers : -1
The product of two numbers : 6
The quotient of x/y : 0
The remainder when x%y : 2
The remainder when x%y : 5
WEEK – 2:
2) a) Write a program to call default constructor first and then any other constructor in the class?
Code:
import java.util.*;
class Student
{
int rollno;
String name;
Student()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the roll num: ");
rollno = sc.nextInt();
System.out.println("Enter the name of student: ");
name = sc.next();
}
Student(int rollno, String name)
{
this.rollno = rollno;
this.name = name;
}
String getStudentDetails()
return rollno +" "+name;
}
class Twoa
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int roll;
String name;
Student s1 = new Student();
System.out.println(s1.getStudentDetails());
System.out.println("Enter the roll num: ");
roll = s.nextInt();
System.out.println("Enter the name of student: ");
name = s.next();
Student s2 = new Student(roll, name);
System.out.println(s2.getStudentDetails());
}
}
Output:
D:\Java>java Twoa
Enter the roll num:
21
Enter the name of student:
rahul
21 rahul
Enter the roll num:
3
Enter the name of student:
santosh
3 santosh
b) Write a program that accepts an array of integers and print those which are both odd and
prime. If no such element in that array print “Not found”.
Code:
import java.util.*;
public class Twob
{
public static void main(String[] args) {
int i, j, a[] = new int[50], count, flag = 0, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
n = sc.nextInt();
System.out.println("enter array elements: ");
for(i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
System.out.println("Odd prime numbers are: ");
for(i =0; i < n; i++)
{
count = 0;
if(a[i] % 2 != 0 && a[i] != 1)
{
flag = 1;
for(j = 2; j <= a[i]; j++)
{
if(a[i] % j == 0)
{
count++;
}
}
if(count == 1)
{
System.out.println(" "+a[i]);
}
}
}
if(flag == 0)
{
System.out.println("Not found!");
}
}
}
Output:
D:\Java>java Twob
Enter number of elements:
5
enter array elements:
12345
Odd prime numbers are:
3
5
c) Write a program to accept contents into an Integer Array and print the frequency of each
number in the order of their number of occurrences.
Code:
import java.util.*;
class Freq1
{
public static void main(String [] args)
{
Scanner scn = new Scanner(System.in);
int i,j,c,n,n1;
System.out.println("Enter the no of elements in the array:");
n=scn.nextInt();
int a[] = new int [n];
int b[] = new int [n];
System.out.println("Enter the elements of the array:");
for(i=0;i<n;i++)
{
a[i]=scn.nextInt();
}
b[0]=a[0];
n1=1;
for(i=1;i<n;i++)
{
c=0;
for(j=0;j<n1;j++)
{
if(a[i]!=b[j])
{
c++;
}
}
if(c==n1)
{
b[n1]=a[i];
n1++;
}
}
for(j=0;j<n1;j++)
{
c=0;
for(i=0;i<n;i++)
{
if(b[j]==a[i])
c++;
}
System.out.println(b[j]+" - "+c);
}
}
}
Output:
D:\Java>java Twoc
Enter the no of elements in the array:
5
Enter the elements of the array:
12234
1 -1
2 -2
3 -1
4 -1
d) Write a program that accepts an „m x n‟ double dimension array, where „m‟ represents
financial years and „n‟ represents Ids of the items sold. Each element in the array represents
number of items sold in a particular year. Identify the year and id of the item which has more
demand.
Code:
import java.util.*;
class Twod
{
public static void main(String []arg)
{
int i,j,m,n,max,y,id;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of financial years :");
m=sc.nextInt();
System.out.println("Enter the no of id's:");
n=sc.nextInt();
}
}
Output:
D:\Java>java Twod1
Enter the no of financial years :
4
Enter the no of id's:
4
Enter the financial years :
2000
2001
2002
2003
Enter the id's :
11
12
13
14
Enter the values in the particular year:
1
2
3
4
5
6
7
8
9
10
1
12
13
14
15
16
The year and id is 2003 14
WEEK – 3
3) a) Create a class Box that uses a parameterized constructor to initialize the dimensions of a box.
The dimensions of the Box are width, height, depth. The class should have a method that can
return the volume of the box. Create an object of the Box class and test the functionalities.
Code:
import java.util.*;
class Box
this.length = length;
this.width = width;
this.height = height;
double Volume()
return length*height*width;
class Threea
double l, w, h, v;
l = sc.nextDouble();
w = sc.nextDouble();
h = sc.nextDouble();
Box n = new Box(l, w, h);
v = n.Volume();
Output:
D:\Java>java Threea
4.3
1.2
3.5
b) Create a new class called Calculator with the following methods: A static method called
powerInt(int num1,int num2) This method should return num1 to the power num2. A static
method called powerDouble(double num1,double num2). This method should return num1 to the
power num2. Invoke both the methods and test the functionality. Also count the number of
objects created
Code:
import java.util.*;
class Calculator
int pow = 1;
pow = pow*num1;
return pow;
}
static double powerDouble(double num1, double num2)
class Threeb
double a, b;
int c, d;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextInt();
d = sc.nextInt();
Output:
3.2
4.0
4) a) Accept a String and a number „n‟ from user. Divide the given string into substrings each of
size „n‟ and sort them lexicographically
Code:
import java.util.*;
class Foura
int i, j;
String s = sc.next();
int n = sc.nextInt();
String l = "";
l = l + s2[j];
Arrays.sort(t);
System.out.println(u);
Output:
D:\Java>java Foura
Enter a string:
compiler
co
mp
il
er
b) Accept an array of strings and display the number of vowels and consonants occurred in each
string
Code:
import java.util.*;
String s = sc.next();
int m = 0 , n = 0;
switch (temp[i])
case 'a':
m++;
break;
case 'e':
m++;
break;
case 'i':
m++;
break;
case 'o':
m++;
break;
case 'u':
m++;
break;
default:
n++;
break;
Output:
D:\Java>java Fourb
import java.util.*;
c) Accept two strings from the user and determine if the strings are anagrams or not
Code:
class Fourc
{
static String str1, str2, str3, str4;
if(str1.length() == str2.length())
Arrays.sort(arr1);
Arrays.sort(arr2);
if(str3.equals(str4))
else
str1 = obj.nextLine();
str2 = obj.nextLine();
anagram(str1,str2);
Output:
D:\Java>java Fourc
raahul
lahaur