0% found this document useful (0 votes)
2 views21 pages

Java Record

The document outlines a series of Java programming exercises across four weeks, focusing on various concepts such as command line arguments, classes and objects, constructors, arrays, and string manipulation. Each exercise includes code examples, expected outputs, and instructions for tasks like calculating the area of a circle, finding odd prime numbers, and checking for anagrams. The content is structured to progressively build programming skills through practical applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Java Record

The document outlines a series of Java programming exercises across four weeks, focusing on various concepts such as command line arguments, classes and objects, constructors, arrays, and string manipulation. Each exercise includes code examples, expected outputs, and instructions for tasks like calculating the area of a circle, finding odd prime numbers, and checking for anagrams. The content is structured to progressively build programming skills through practical applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

WEEK – 1

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:

// Using Command Line Arguments :

class Onea

public static void main(String[] args) {

System.out.println(args[0]+" and "+args[1]);

Output:
D:\Java>java Onea

Java programming

Java and programming

a) //Using Scanner class


import java.util.Scanner;
class Onea2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String s1, s2;
System.out.println("Enter the strings s1 and s2 : ");
s1 = scan.nextLine();
s2 = scan.nextLine();
System.out.println(s1 + " and " + s2);

}
OUTPUT :

D:\Java>java Onea2

Enter the two strings s1 and s2:

Java

Programming

Java and Programming

ii) To find the perimeter and area of a circle given a value of radius.

class Onea3

public static void main(String[] args)

double r = Double.parseDouble(args[0]);

System.out.println("Area of cirle is : " + r*r);

System.out.println("Perimeter of circle is : " +2*3.14*r);

Output:

D:\Java>java Onea3

Area of circle is : 36.0

Perimeter of circle is : 37.6669

//Using Scanner Class

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();

int [][]a =new int[m+1][n+1];


System.out.println("Enter the financial years :");
for(i=1;i<m+1;i++)
{
a[i][0]=sc.nextInt();
}
System.out.println("Enter the id's :");
for(j=1;j<n+1;j++)
{
a[0][j]=sc.nextInt();
}
System.out.println("Enter the values in the particular year:");
for(i=1;i<m+1;i++)
{
for(j=1;j<n+1;j++)
{
a[i][j]=sc.nextInt();
}
}
max=a[1][1];
y=a[0][0];
id=a[0][0];
for(i=1;i<m+1;i++)
{
for(j=1;j<n+1;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
y=a[i][0];
id=a[0][j];
}
}
}

System.out.println("The year and id is "+y+" "+id);

}
}
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

double length, width, height;

Box(double length, double width, double height)

this.length = length;

this.width = width;

this.height = height;

double Volume()

return length*height*width;

class Threea

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

double l, w, h, v;

System.out.println("Enter the length value: ");

l = sc.nextDouble();

System.out.println("Enter the width value: ");

w = sc.nextDouble();

System.out.println("Enter the height value: ");

h = sc.nextDouble();
Box n = new Box(l, w, h);

v = n.Volume();

System.out.println("Enter volume is: "+v);

Output:

D:\Java>java Threea

Enter the length value:

4.3

Enter the width value:

1.2

Enter the height value:

3.5

Enter volume is: 18.06

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

static int powerInt(int num1, int num2)

int pow = 1;

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

pow = pow*num1;

return pow;

}
static double powerDouble(double num1, double num2)

return Math.pow(num1, num2);

class Threeb

public static void main(String[] args)

double a, b;

int c, d;

Scanner sc = new Scanner(System.in);

a = sc.nextDouble();

b = sc.nextDouble();

System.out.println("The value raised to power num2 is: "+Calculator.powerDouble(a, b));

c = sc.nextInt();

d = sc.nextInt();

System.out.println("The value raised to power num2 is: "+Calculator.powerInt(c, d));

Output:

3.2

4.0

The value raised to power num2 is: 104.85760000000002

The value raised to power num2 is: 16


WEEK – 4:

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

public static void main (String args[])

int i, j;

Scanner sc = new Scanner (System.in);

System.out.println("Enter a string: ");

String s = sc.next();

System.out.println("Enter the number to divide: ");

int n = sc.nextInt();

for(i = 0; i < s.length(); i = i + n)

char s2[] = s.toCharArray();

String l = "";

for(j = i; j < i + n; j++)

l = l + s2[j];

char t[] = l.toCharArray();

Arrays.sort(t);

String u = new String(t);

System.out.println(u);

Output:
D:\Java>java Foura

Enter a string:

compiler

Enter the number to divide:

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.*;

public class Fourb

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter the string: ");

String s = sc.next();

int m = 0 , n = 0;

String s1= s.toLowerCase();

char[] temp = s1.toCharArray();

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

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;

System.out.println("The Number of vowels are: " + m);

System.out.println("The Number of consonants are: " + n);

Output:

D:\Java>java Fourb

Enter the string: rahul

The Number of vowels are: 2

The Number of consonants are: 3

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;

static void anagram(String str1, String str2)

if(str1.length() == str2.length())

char[] arr1 = str1.toCharArray();

Arrays.sort(arr1);

str3 = new String(arr1);

char[] arr2 = str2.toCharArray();

Arrays.sort(arr2);

str4 = new String(arr2);

if(str3.equals(str4))

System.out.println(""+str1+" and "+str2+" are anagrams.");

else

System.out.println(""+str1+" and "+str2+" are not anagrams.");

public static void main(String args[])

Scanner obj = new Scanner(System.in);

System.out.println("Enter two strings: ");

str1 = obj.nextLine();

str2 = obj.nextLine();

anagram(str1,str2);

Output:
D:\Java>java Fourc

Enter two strings:

raahul

lahaur

raahul and lahaur are anagrams.

You might also like