JAVA PROGRAMMING LAB(NEP)

 1. Write a simple java application, to print the message, “Welcome to java”

public class welcome
{
public static void main(String args[])
{
System.out.println("welcome to java");
}
}

2. Write a program to display the month of a year. Months of the year should be held in an array.

import java.util.Calendar;

public class dateDemo
{
private static String[] month;

public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
String[] month = {"January","February","March"
,"April","May","June","July","August","September","October"
,"November","December"};
System.out.print("Current Month = " + month[calendar.get(Calendar.MONTH)]);
}
}

3. Write a program to demonstrate a division by zero exception

public class DivisionByZeroDemo
{
public static void main(String args[])
{
int a = 5;
int b = 5;
try
{
System.out.println(a/b);
} catch (ArithmeticException e)
{
System.out.println("Division by Zero is not possible");
}
}
}

4. Write a program to create a user defined exception say Pay Out of Bounds. 

import java.util.*;

class PayoutOfBoundsException extends Exception
{
PayoutOfBoundsException(String msg)
{
System.out.println("Pay Out of Bounds Exception :"+ msg);
}

}
public class UserDefinedExceptionDemo
{
public static void main(String argv[]) throws PayoutOfBoundsException
{
System.out.println("Enter the Employee Salary:");
try (Scanner sc = new Scanner(System.in)) {
int Pay = sc.nextInt();
if(Pay < 10000 || Pay > 5000)
{
throw new PayoutOfBoundsException("Salary not in the valid range");
} else
System.out.println("Employee eligible for 30% hike");
}
}
}

5. Write a java program to add two integers and two float numbers. When no arguments are supplied, give a default value to calculate the sum. Use function overloading.

public class MethodOverloadingDemo
{
int addition()
{
return(10+10);
}
int addition(int x, int y)
{
return(x+y);
}
float addition(float a, float b)
{
return(a +b);
}
public static void main(String args[])
{
MethodOverloadingDemo a = new MethodOverloadingDemo();
System.out.println(" By Using Default Values , Sum is :"+ a.addition());
System.out.println("Sum of Two Integer Values (10 and 20 )"+a.addition(10,20));
System.out.println("Sum of Two Float Vales (10.5 and 20.5) is "+a.addition(10.2f,20.3f));
}

6. Write a program to perform mathematical operations. Create a class called AddSub with methods to add and subtract. Create another class called MulDiv that extends from AddSub class to use the member data of the super class. MulDiv should have methods to multiply and divide A main function should access the methods and perform the mathematical operations.

class AddSub {
int n1,n2;

public AddSub(int x, int y)
{
n1 = x;
n2 = y;
}
public int add()
{
return(n1 + n2);
}
public int sub(){
return(n1 - n2);
}
}

class MulDiv extends AddSub
{
public MulDiv(int x , int y)
{
super(x, y);
}
public int mul()
{
return(n1 * n2);
}

public int div()
{
return(n1/n2);
}
}

public class AirthmeticOperations
{
public static void main(String args[])
{
MulDiv obj = new MulDiv(20, 10);
System.out.println("Sum of 20 and 10 ="+ obj.add());
System.out.println("Difference of 20 and 10 ="+ obj.sub());
System.out.println("Product of 20 and 10 ="+ obj.mul());
System.out.println("Result of division of 20/10 ="+ obj.div());

}
}

7. Write a program with class variable that is available for all instances of a class. Use staticvariable declaration. Observe the changes that occur in the object’s member variable values

class Student
{
static String collegeName = "Williams college";
int rollNO;
String name;

Student(int rollno, String name)
{
this.rollNO = rollno;
this.name = name;
}

void display()
{
System.out.print(collegeName+" "+rollNO+""+ name);
}
}

public class StaticDemo
{
public static void main(String args[])
{
System.out.println("Object Sharing the Static Variable - College Name \n");
Student s1 = new Student(1001, "godfather");
Student s2 = new Student(1002, "comrade");
s1.display();
s2.display();
System.out.println("\n Static value Changed by One of the Object \n");
s1.collegeName = "Benguluru city university";
s1.display();
s2.display();
}

}

8. Write a java program to create a student class with following attributes: Enrollment_id: Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three marks must be calculated only when the student passes in all three subjects. The pass mark for each subject is 50. If a candidate fails in any one of the subjects his total mark must be declaredas zero. Using this condition write a constructor for this class. Write separate functions for accepting and displaying student details. In the main method create an array of three student objects and display the details.

import java.util.*;
class Student
{
Scanner sc = new Scanner(System.in);
String Enrollment_id;
String Name;
int sub1, sub2, sub3, total;
Student()
{
readStudentInfo();
}
private void readStudentInfo()
{
System.out.println("Enter Student Details");
System.out.println("EnrolmentNo:");
Enrollment_id = sc.next();
System.out.println("Name: ");
Name = sc.next();
System.out.println("Enter Marks of 3 Subjects: ");
sub1 = sc.nextInt();
sub2 = sc.nextInt();
sub3 = sc.nextInt();
if(sub1 >= 50 && sub2 >= 50 && sub3 >= 50)
total = sub1 + sub2 + sub3;
else
total = 0;
}
public void displayInfo()
{
System.out.println(Enrollment_id+"\t\t"+Name+"\t"+total);
}
}
public class StudentInfo
{
public static void main(String args[])
{
Student s[] = new Student[3];
for(int i = 0; i < 3; i++)
{
s[i] = new Student();
}
System.out.println("\t\t Student Details");
System.out.println("EnrollmentNo\tName\tTotal");
for(int i = 0; i<3; i++)
{
s[i].displayInfo();
}
}
}

9. In a college first year class are having the following attributesName of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class


10. Define a class called first year with above attributes and define a suitable constructor. Also write a method called best Student () which process a first-year object and return the student with the highes

Comments

Popular posts from this blog

Mobile Application Development Lab

WEB PROGRAMMING LAB (html&php)

Python Programming