BK Module 1 Java
BK Module 1 Java
Date:
Question 1:
Write a program to accept gender ("Male" or "Female") and age (1-120) from command line
arguments and print the percentage of interest based on the given conditions.
Interest == 8.2% Gender ==> Female Age ==>1 to 58 Interest == 7.6% Gender
==> Female Age ==>59 -120Interest == 9.2% Gender ==> Male Age ==>1-60
Aim:
To develop java program for printing the percentage of interest based on the given conditions.
Code:
package module1;
int Age=Integer.parseInt(args[0]);
if(Age>=120) {
System.out.println("age is > 120");
}
else if(Age>=1 && Age<=58) {
System.out.println("Interest == 8.2% Gender");
}
else if(Age>=59 && Age<=120) {
System.out.println("Interest == 7.6% Gender");
}
else if(Age>=1 && Age<=58) {
System.out.println("Interest == 9.2% Gender");
}
else if(Age>=58 && Age<=120) {
System.out.println("Interest == 9.2% Gender");
}
717822L210
Output:
Result:
Thus,the java program to find the interest percentage of the user is compiled,executed and verified
successfully.
717822L210
667t
717822L210
667t
Question 2:
Write a program to convert from upper case to lower case and vice versa of an alphabet andprint the old
Aim:
To develop java program for converting from upper case to lower case and vice versa of an alphabet andprint
Code:
package module1;
import java.util.Scanner;
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("Uppercase_Lowercase");
Scanner et=new
Scanner(System.in); char
alphabet=et.next().charAt(0); int
Al=alphabet+32;
int La=alphabet-32;
if(alphabet>95){
System.out.println((char)(La));
}
else{
System.out.println((char)(Al));
}
}
}
717822L210
717822L205
Output:
Result:
Thus,the java program to conver the lower case character to upper case is compiled,executed and verified
successfully.
717822L210
667t
717822L205
Question 3:
Aim:
Code:
package module1;
import java.util.Scanner;
public class primenumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("Check Prime Number or Not");
Scanner scr=new Scanner(System.in);
int count=0;
int N=scr.nextInt();
if(N>1) {
for(int i=1;i<=N;i++) {
if(N%i==0) {
count++;
}
}
if(count==2) {
System.out.println(N+ " is a prime number");
}
else {
System.out.println(N+ " is not a prime number");
}
}
else{
System.out.println(" not prime number");
}
}
}
717822L210
667t
Output:
Result:
Thus,the java program to find the given number number is prime or not is compiled,executed and verified
successfully.
717822L210
717822L205
Question 4:
Write a program to print even numbers between 23 and 57, each number should be printed in a separate
row.
Aim:
To develop java program for printing even numbers between 23 and 57, each number should be printed
in a separate row.
Code:
package module1;
if(i%2==0)
{
System.out.println(i);
}
}
}
717822L210
667t
Output:
Result:
Thus,the java program to print even numbers between 23 and 57 is compiled,executed and verified
successfully.
717822L210
717822L205
667t
Question 5:
Aim:
To develop java program for finding if the given number is prime or not.
Code:
package module1;
int m=0;
for(int i=2;i<a;i++) {
if(a%i==0) {
m=+1;
}
}
if(m==0) {
System.out.println(a+ " is a prime number ");
}
else
{ System.out.println(a+ " is not a prime number");
}
}
}
717822L210
717822L205
717822L205
667t
Output:
Result:
Thus,the java program to find given number is prime or not is compiled,executed and verified successfully.
717822L210
717822L205
717822L205
667t
Question 6:
Write a program to print * in Floyds format (using for and while loop)
Aim:
Code:
}
System.out.println();
}
717822L210
717822L210
667t
Output:
Result:
Thus,the java program to print floyds pattern is compiled,executed and verified successfully.
717822L210
717822L210
667t
Question 7:
Aim:
Code:
public class reverse {
System.out.println("717822l210");
System.out.println("Dheivani M");
System.out.println("Given order digit");
int num=Integer.parseInt(args[0]); int
rem=0;
int rev=0;
while(num>0) {
rem=num%10;
rev=rev*10+rem;
num/=10;
}
System.out.println(rev);
}
717822L210
717822L210
667t
Output:
Result:
Thus,the java program to reverse the given number is compiled,executed and verified
successfully.
717822L210
717822L210
667t
Question 8:
Write a program that displays a menu with options 1. Add 2. Sub Based on the options chosen, read 2
numbers and perform the relevant operation. After performing the operation, the program should ask the
user if he wants to continue. If the user presses y or Y, then the program should continue displaying the
Aim:
To develop java program that displays a menu with Add and Subtract options.
Code:
package module1;
import java.util.Scanner;
public class switch_case {
public static void main(String[] args) {
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("Switch_Case");
char ch;
do {
int choice;
System.out.println("Enter 2 numbers");
Scanner scr = new Scanner(System.in);
int a= scr.nextInt();
int b= scr.nextInt();
System.out.println("Enter choice");
System.out.println("Press 1 for an
Addition");
System.out.println("Press 2 for an Subraction");
choice = scr.nextInt();
switch(choice) {
case 1:
int
res;
res=a+b
;
System.out.println(res);
break;
case 2:
int
res1;
res1=a-
b;
System.out.println(res1);
break;
}
System.out.println("if you want to continue,Enter 'Y' or'y'
character");
ch =scr.next().charAt(0);
717822L205
}
while(ch == 'y' || ch =='Y'); }
717822L210
717822L210
OUTPUT:
Result:
Thus,the java program to perform addition and subtraction operation using switch case is
717822L210
717822L205
717822L210
Question 9:
Aim:
Code:
717822L210
717822L205
Output:
Result:
Thus,the java program to print the number which are divisible by 2,3 and 5 was executed
successfully and the output was verified.
717822L205
717822 L205
EX NO:2
PROGRAM USING ARRAY
DATE:
10. Question:
Write a java program to check whether the number is present in the array or not.
Aim:
To develop java program to check whether the target number is present or not.
Code:
package module1;
import java.util.Scanner;
public class array_searching {
public static void main(String[] args) {
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("array_sarching");
int arr[]=new int[] {1,2,4,6,8,12}; int
count=0;
Scanner scr=new Scanner(System.in);
System.out.println("Enter the search element");
int k=scr.nextInt();
for(int i=0;i<5;i++)
{
if(arr[i]==k)
{
System.out.println("Index="+ (i+1));
count++;
}
}
if(count==0)
{
System.out.println("-1");
}
}
}
717822L205
717822 L205
Output:
Result:
Thus, the java program to find the target element is present or not was executed successfully and the
Write a program to remove the duplicate elements in an array and print Eg)
Aim:
To develop java program to remove duplicate element from the given array.
Code:
package module1;
import java.util.Scanner;
public class duplicates {
public static void main(String[] args) {
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("Remove duplicates Element From an Array");
System.out.println("Enter array of the size");
Scanner scr=new Scanner(System.in);
int n=scr.nextInt();
int arr[]=new int[n];
int b[]=new int[n];
int count=0,k=0;
System.out.println("Enter array Elements");
for(int i=0;i<n;i++)
{
arr[i]=scr.nextInt();
}
for(int i=0;i<n;i++)
{
count=0;
for(int j=i+1;j<n;j++)
{
if (arr[i]==arr[j])
{
count++;
break;
}
}
if(count==0)
{
b[k]=arr[i];
k++;
}
}
System.out.println("The new array is");
for(int i=0;i<k;i++)
{
System.out.print(" "+b[i]);
}
}
717822L210
717
Output:
Result:
Thus, the java program to remove the duplicate element in the array was executed successfully and the
717822L210
12. Question:
Write a program to print the sum of the elements of the array with the given below condition. If the
array has 6 and 7 in succeeding orders,ignore 6 and 7 and the numbers between them for the
calculation of Sum.
Aim:
To develop java program to calculate the sum of elements in an array based on the condition.
Code:
package module1;
import java.util.Scanner;
public class sum_of_array {
717822L210
Output:
Result:
Thus, the java program to find the sum of array for specific condition was executed successfully and the
717822L210
13. Question:
Write a program to reverse the elements of a given 2*2 array based on command line arguments.
Aim:
To develop java program to reverse the elements of a given 2*2 array based on
Code:
package module1;
public class reverse_array {
public static void main(String[] args) {
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("Reverse of the Array");
int n;
n=args.length;
if(n!=4)
{
System.out.println("Enter valid number");
}
if(n==4)
{
int arr[][]=new int [2][2];
int i,j,p=0; for(i=0;i<2;i+
+)
{
for(j=0;j<2;j++)
{
arr[i][j]=Integer.parseInt(args[p]);
p++;
}
}
System.out.println("The giveen array is:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(" "+arr[i][j]);
}
}
System.out.println();
System.out.println("The reverse of the array is:");
for(i=1;i>=0;i--)
{
for(j=1;j>=0;j--)
{
System.out.print(" "+arr[i][j]);
}
}
}
}
}
717822L210
Output:
Result:
Thus, the java program to reverse the 2*2 array was executed successfully and the output was verified.
717822L210
14. Question:
Write a program to find greatest number in a 3*3 array based on the command line arguments.
Aim:
To develop java program to find greatest number in a 3*3 array based on the command line
arguments.
Code:
package module1;
public class max_of_array{
public static void main(String[] args) {
System.out.println("717822L210");
System.out.println("Dheivani M");
if (args.length != 9) {
System.out.println("Please enter 9 integer numbers");
return;
}
int[][] arr = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = Integer.parseInt(args[i * 3 + j]);
}
}
int largestNumber = arr[0][0];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] > largestNumber) {
largestNumber = arr[i][j];
}
}
}
System.out.println("The given array is:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("The biggest number in the given array is " +
largestNumber);
}
717822L210
Output:
Result:
Thus, the java program to find the maximum element in the given 3*3 array is was executed
717822L210
Ex no:03 PROGRAMS ON CLASSES AND OBJECTS
Date:
Question 15:
Create a new class called “Calculator” which contains the following: A static method called powerInt(int
num1,int num2) that accepts two integers andreturns num1 to the power of num2 (num1 power num2).A
static method called powerDouble(double num1,int num2) thataccepts one doubleand one integer and
returns num1 to the power of num2 (num1power num2).Call your method from another class without
instantiating the class (i.e. call it like Calculator.powerInt(12,10) since your methods are defined to be static)
Aim:
To develop java program to find the power of numbers using classes and objects.
Code:
package module1;
}
717822L210
Output:
Result:
Thus,the java program to calculate the power of the numbers using classes and objects was executed
successfully and the output was verified.
717822L210
Question 16:
Write a java program to Calculate the BMI of the patient using classes and objects. BMI can be
calculated as, BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703.
Aim:
To develop java program for calculating the BMI of the patient using classes and objects.
Code:
package module1;
}
package module1;
import java.util.Scanner;
public class BMIDemo {
717822L210
717822L205
Output:
Result:
Thus,the java program to calculate the BMI of the patient using classes and objects was executed
717822L210
Question 17:
• Write a Java class for the Circle class based on the UML class diagram.
• Write a main class TestCircle that creates two objects for the Circle class and invoke all the
methods.
Aim:
To develop java program to find the area of the circle using classes and objects.
Code:
package module1;
public circle()
{
this.radius=1.0;
this.colour="red";
}
public circle(double radius)
{
this.radius=radius;
}
public circle(double radius,String colour)
{
this.radius=radius;
this.colour=colour;
}
public double getRadius() {
return radius;
Salini V
} 717822l210
public void setRadius(double radius) {
this.radius=radius;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour=colour;
}
public double getArea() {
return 3.14*this.radius*this.radius;
}
public double getCircumference() {
return 2*3.14*this.radius;
}
public String toString () {
return "circle[radius="+radius+"colour="+colour+"]";
}
717822L210
Salini V
Output:
Result:
Thus,the java program to calculate the Area of the circle using classes and objects was executed
717822 L205
717822L210
717822 L205
Question 18:
Write a Java class for the Rectangle class based on the UML class diagram. • Write a main class TestRectangle
that creates two objects for the Rectangle class and invoke all the methods.
Aim:
To develop java program for calculating the area and perimeter of the rectangle using classes and objects.
Code:
package module1;
717822L210
{
return width;
}
public void setWidth(float width)
{
this.width=width;
}
public double getArea()
{
return length*width;
}
public double getPermiter()
{
return 2*(length+width);
}
public String toString()
{
return "Rectangle[length="+length+",width="+width+"]";
}
}
package module1;
import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("717822L205");
System.out.println("BARATHKUMAR V");
System.out.println("find Area and Perimeter of the rectangle using classes and
Objects");
717822L210
717822 L205
OUTPUT:
Result:
Thus,the java program to Calculate the Area and perimeter of the Rectangle using classes and objects was
executed successfully and the output was verified.
717822L210
717822L205
717822L205
Question 19:
• Write a Java class for the Employee class based on the UML class diagram.
• Write a main class TestEmployee that creates two objects for the Employee class and invoke all the
methods.
Aim:
To develop java program to find the total salary,Salary increment percentage of the employee using classes
and objects.
Code:
package module1;
717822L205
{
return id;
}
public String getFirstname()
{
return firstname;
}
public String getLastname()
{
return lastname;
}
public int getSalary()
{
return salary;
}
public void setId(int id)
{
this.id=id;
}
public void setFirstname(String firstname)
{
this.firstname=firstname;
}
public void setLastname(String lastname)
{
this.lastname=lastname;
}
public void setSalary(int salary)
{
this.salary=salary;
}
public int getAnnualsalary()
{
return salary*12;
}
public int getRaisesalary(int percent)
{
int a=salary*percent/100;
return a+salary;
}
public String toString()
{
return
"Employee[id="+id+",name="+firstname+lastname+",salary="+salary+"]";
}
package module1;
import java.util.Scanner;
public class Testemployee {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("Program to calculate the total salary and
increment percentage");
System.out.println("of the employee using classes and objects.");
employee e1 = new employee();
e1.setId(154); 717822L210
e1.setFirstname("swathi");
e1.setLastname("s");
e1.setSalary(15000);
System.out.println("ID:"+e1.getId());
System.out.println("Firstname:"+e1.getFirstname());
System.out.println("Lastname:"+e1.getLastname());
System.out.println("Salary:"+e1.getSalary());
System.out.println("Annual Salary:"+e1.getAnnualsalary());
System.out.println("Increment salary:"+e1.getRaisesalary(50));
System.out.println("Enter id,firstname,lastname and salary");
Scanner scr=new Scanner(System.in);
int id=scr.nextInt();
String firstname=scr.next();
String lastname=scr.next();
int salary=scr.nextInt();
employee e2 = new employee(id,firstname,lastname,salary);
System.out.println("ID:"+e2.getId());
System.out.println("Firstname:"+e2.getFirstname());
System.out.println("Lastname:"+e2.getLastname());
System.out.println("Salary:"+e2.getSalary());
System.out.println("Annual Salary:"+ e2.getAnnualsalary());
System.out.println("Increment Salary:"+e2.getRaisesalary(50));
System.out.println(e1);
System.out.println(e2);
}
717822L210
717822L205
717822L210
OUTPUT:
Result:
Thus,the java program to calculate the total salary and salary increment percentage of the employee was executed
717822L205
717822L210
Question 20:
• Write a Java class for the InvoiceItem class based on the UML class diagram.
• Write a main class TestInvoiceItem that creates two objects for the Employee class and invoke all the methods.
Aim:
To develop java program for the InvoiceItem UML class diagram by using classes and objects.
Code:
package module1;
717822L210
717822L210
}
public String getId()
{
return id;
}
public String getDesc()
{
return desc;
}
public int getQty()
{
return qty;
}
public double getUnitprize()
{
return unitprize;
}
public void setId(String id)
{
this.id=id;
}
public void setDesc(String desc)
{
this.desc=desc;
}
public void setQty(int qty)
{
this.qty=qty;
}
public void setUnitprize(double unitprize)
{
this.unitprize=unitprize;
}
public double getTotal()
{
return unitprize*qty;
}
public String toString()
{
return
"InvoiceItem=[id="+id+",desc="+desc+",qty="+qty+",unitprize="+unitprize+
"]";
}
a
}
package module1;
import java.util.Scanner;
public class testinvoice {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("717822L210");
System.out.println("Dheivani M");
System.out.println("find the total prize using class and object");
717822L210
717822L210
717822L205
OUTPUT:
717822L210
717822L210
717822L205
717822L210
Result:
Thus,the java program to invoke all the methods in the UML diagram for InvoiceItem was executed
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF FACULTY
717822L205