java program for semester first, Assignments of Java Programming

this helps to submit java practicals for sem 1

Typology: Assignments

2020/2021

Uploaded on 05/22/2021

shubhamlagad
shubhamlagad 🇮🇳

1 document

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Java Tools and IDE, Simple java programs
a. Write a java program to display the system date and time in various formats shown
below:
Current date is : 31/07/2015
Current date is : 07-31-2015
Current date is : Friday July 31 2015
Current date and time is : Fri July 31 16:25:56 IST 2015
Current date and time is : 31/07/15 16:25:56 PM +0530
Current time is : 16:25:56
Current week of year is : 31
Current week of month : 5
Current day of the year is : 212
Note: Use java.util.Date and java.text.SimpleDateFormat class
Program :
import java.util.Date;
import java.text.SimpleDateFormat;
public class Ass1SetB1 {
public static void main(String args[])
{
Date d = new Date();
SimpleDateFormat obj;
String Date;
obj = new SimpleDateFormat("dd/MM/yyyy");
Date = obj.format(d);
System.out.print("Current date is :");
System.out.println(Date);
obj = new SimpleDateFormat("MM-dd-yyyy");
Date = obj.format(d);
System.out.print("Current date is :");
System.out.println(Date);
obj = new SimpleDateFormat("EEEE MMMM dd yyyy");
Date = obj.format(d);
System.out.print("Current date is :");
System.out.println(Date);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download java program for semester first and more Assignments Java Programming in PDF only on Docsity!

1. Java Tools and IDE, Simple java programs a. Write a java program to display the system date and time in various formats shown below: Current date is : 31/07/ Current date is : 07- 31 - 2015 Current date is : Friday July 31 2015 Current date and time is : Fri July 31 16:25:56 IST 2015 Current date and time is : 31/07/15 16:25:56 PM + Current time is : 16:25: Current week of year is : 31 Current week of month : 5 Current day of the year is : 212 Note: Use java.util.Date and java.text.SimpleDateFormat class

Program :

import java.util.Date; import java.text.SimpleDateFormat; public class Ass1SetB1 { public static void main(String args[]) { Date d = new Date(); SimpleDateFormat obj; String Date; obj = new SimpleDateFormat("dd/MM/yyyy"); Date = obj.format(d); System.out.print("Current date is :"); System.out.println(Date); obj = new SimpleDateFormat("MM-dd-yyyy"); Date = obj.format(d); System.out.print("Current date is :"); System.out.println(Date); obj = new SimpleDateFormat("EEEE MMMM dd yyyy"); Date = obj.format(d); System.out.print("Current date is :"); System.out.println(Date);

obj = new SimpleDateFormat("EEE MMMM dd HH:MM:ss z yyyy"); Date = obj.format(d); System.out.print("Current date and time is :"); System.out.println(Date); obj = new SimpleDateFormat("MM/dd/yy HH:MM:ssa Z"); Date = obj.format(d); System.out.print("Current date and time is :"); System.out.println(Date); obj = new SimpleDateFormat("HH:MM:ss"); Date = obj.format(d); System.out.print("Current time is :"); System.out.println(Date); obj = new SimpleDateFormat("w"); Date = obj.format(d); System.out.print("Current week of the year is :"); System.out.println(Date); obj = new SimpleDateFormat("W"); Date = obj.format(d); System.out.print("Current week of the month is :"); System.out.println(Date); obj = new SimpleDateFormat("D"); Date = obj.format(d); System.out.print("Current day of the year is :"); System.out.println(Date); } }

Output :

Current date is :14/05/ Current date is :05- 14 - 2021 Current date is :Friday May 14 2021 Current date and time is :Fri May 14 08:05:05 IST 2021 Current date and time is :05/14/21 08:05:05am + Current time is :08:05: Current week of the year is : Current week of the month is :

} void isOdd() { if(num% 2 != 0 ) { System.out.println("Number is odd..."); } } void isEven() { if(num% 2 == 0 ) { System.out.println("Number is even..."); } } } public class Ass1SetB { public static void main(String args[]) { int number = Integer.parseInt(args[ 0 ]); // MyNumber n = new MyNumber(); MyNumber n = new MyNumber(number); n.isNegative(); n.isPositive(); n.isZero(); n.isOdd(); n.isEven(); } }

Output :

PS D:\JavaProgram> javac Ass1SetB2.java PS D:\JavaProgram> java Ass1SetB2 5 Number is positive... Number is odd... PS D:\JavaProgram> java Ass1SetB2 - 8 Number is negative... Number is even... PS D:\JavaProgram> java Ass1SetB2 0 Number is zero... Number is even...

PS D:\JavaProgram> java Ass1SetB2 - 0 Number is zero... Number is even... PS D:\JavaProgram>

2. Array of Objects and Packages a. Modify the above program to create n objects of the Student class. Accept details from the user for each object. Define a static method “sortStudent” which sorts the array on the basis of percentage.

Program :

import java.util.*; class Students { public int rollno; public String name; public float percentage; Students() { rollno = 10 ; name = "shubham"; percentage = 88.80f; } Students(int rollno,String name,float percentage) { this.rollno = rollno; this.name = name; this.percentage = percentage; } public String toString() { return "[ Roll no : " + rollno + ",\tName : " + name + ",
tPercentage : " + percentage +"]"; } public static void sortStudent(Students s[],int n) { Students temp = new Students(); for(int j= 0 ;j<n- 1 ;j++) for(int i=j+ 1 ;i<n;i++) { if(s[j].percentage>s[i].percentage) { temp=s[j]; s[j] = s[i];

Enter name : shubham Enter percentage : 74. =========Enter details of 1 student============== Enter rollno : 70 Enter name : abhishek Enter percentage : 65. =========Enter details of 2 student============== Enter rollno : 72 Enter name : sagar Enter percentage : 70. Details before sort [ Roll no : 69, Name : shubham, Percentage : 74.73] [ Roll no : 70, Name : abhishek, Percentage : 65.66] [ Roll no : 72, Name : sagar, Percentage : 70.73] Details after sort [ Roll no : 70, Name : abhishek, Percentage : 65.66] [ Roll no : 72, Name : sagar, Percentage : 70.73] [ Roll no : 69, Name : shubham, Percentage : 74.73] PS D:\JavaProgram> b. Create a package named Series having three different classes to print series: a. Prime numbers b. Fibonacci series c. Squares of numbers Write a program to generate ‘n’ terms of the above series.

Program :

Package :

1.Prime numbers

package series;

public class PrimeNumber { public PrimeNumber(int n) { int flag; System.out.print("\nThe prime numbers upto "+n+" are : "); for(int i= 2 ;i<=n;i++) { flag = 1 ; for(int j= 2 ;j<=i/ 2 ;j++) { if(i%j== 0 ) { flag= 0 ; break; } } if(flag == 1 ) System.out.print(i + " "); } } }

2.Fibnocci

package series; public class Fibnocci { public Fibnocci(int n) { // int n=10; int f[] = new int[n]; System.out.print("\nThe fibnocci series upto "+n+" are : "); for(int i= 0 ;i<n;i++) { f[i]=i; } for(int i= 0 ;i<n;i++) { if(i== 0 || i== 1 ) { f[i]=i; System.out.print(f[i]+" "); } else

Output :

Enter number to find given series : 15 The prime numbers upto 15 are : 2 3 5 7 11 13 The fibnocci series upto 15 are : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 Square of numbers series upto 15 are : 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 PS D:\JavaProgram>

3. Inheritance and Interfaces

a. Create an abstract class Shape with methods calc_area and calc_volume. Derive three classes Sphere(radius) , Cone(radius, height) and Cylinder(radius, height), Box(length, breadth, height) from it. Calculate area and volume of all. (Use Method overriding).

Program :

import java.util.; abstract class Shape { abstract void calc_area(); abstract void calc_volume(); } class Sphere extends Shape { double r; Sphere(double r) { this.r = r; } void calc_area() { double a = 4 3.14rr; System.out.print("\nArea of sphere is : "+a); } void calc_volume() { double v = ( 4 / 3 )3.14rrr;

System.out.print("\nVolume of sphere is : "+v); } } class Cone extends Sphere { double r; double h; Cone(double r,double h) { super(r); this.r = r; this.h = h; } void calc_area() { super.calc_area(); double a = 3.14r(r + Math.sqrt(hh + rr)); System.out.print("\nArea of cone is : "+a); } void calc_volume() { super.calc_volume(); double v = 3.14rrh/ 3 ; System.out.print("\nVolume of cone is : "+v); } } class Cylinder extends Cone { double r; double h; Cylinder(double r,double h) { super(r, h); this.r = r; this.h = h; } void calc_area() { super.calc_area(); double a = 2 3.14rh + 2 3.14rr; System.out.print("\nArea of cylinder is : "+a); } void calc_volume() { super.calc_volume(); double v = 3.14rrh; System.out.print("\nVolume of cylinder is : "+v);

Area of box is : 62. Volume of sphere is : 25. Volume of cone is : 20. Volume of cylinder is : 62. Volume of box is : 30. PS D:\JavaProgram> b. Define an abstract class “Staff” with members name and address. Define two subclasses of this class – “FullTimeStaff” (department, salary) and “PartTimeStaff” (number-of-hours, rate-perhour). Define appropriate constructors. Create n objects which could be of either FullTimeStaff or PartTimeStaff class by asking the user’s choice. Display details of all “FullTimeStaff” objects and all “PartTimeStaff” objects.

Program :

import java.util.Scanner; abstract class Staff { Scanner sc = new Scanner(System.in); String name; String address; abstract void accept(); abstract void display(); } class FullTimeStaff extends Staff { String department; int salary; void accept() { System.out.print("\nEnter the name of Staff member : "); name = sc.next(); System.out.print("\nEnter the address of Staff member : "); address = sc.next(); System.out.print("\nEnter the department of Staff member : "); department = sc.next(); System.out.print("\nEnter the salary of Staff member : "); salary = sc.nextInt(); } void display() { System.out.print("\nName of Staff member : "+name);

System.out.print("\nAddress of Staff member : "+address); System.out.print("\nDepartment of Staff member : "+department); System.out.print("\nSalary of Staff member : "+salary); } } class PartTimeStaff extends Staff { int number_of_hours; int rate_perhour; void accept() { System.out.print("\nEnter the name of Staff member : "); name = sc.next(); System.out.print("\nEnter the address of Staff member : "); address = sc.next();; System.out.print("\nEnter the number_of_hours of Staff member : ") ; number_of_hours = sc.nextInt(); System.out.print("\nEnter the rate_perhour of Staff member : "); rate_perhour = sc.nextInt(); } void display() { System.out.print("\nName of Staff member : "+name); System.out.print("\nAddress of Staff member : "+address); System.out.print("\nNumber_of_hours of Staff member : "+number_of_ hours); System.out.print("\nRate_perhour of Staff member : "+rate_perhour) ; } } public class Ass3SetB { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("\nEnter how many Staffs members : "); int n = sc.nextInt(); Staff[] S = new Staff[n]; for(int i= 0 ;i<n;i++) { System.out.print("Enter type of Staff(1 for FullTime/ 2 fo r PartTime) : "); int type = sc.nextInt(); if(type== 1 ) { S[i] = new FullTimeStaff(); S[i].accept(); }else if(type== 2 ) { S[i] = new PartTimeStaff(); S[i].accept(); } } for(int i= 0 ;i<n;i++) {

Enter the rate_perhour of Staff member : 300 Enter type of Staff(1 for FullTime/ 2 for PartTime) : 1 Enter the name of Staff member : abhishek Enter the address of Staff member : nagar Enter the department of Staff member : electronic Enter the salary of Staff member : 20000 ====Details of FullTime Staff==== Name of Staff member : shubham Address of Staff member : nagar Department of Staff member : computer Salary of Staff member : 15000 =================================== ====Details of PartTime Staff==== Name of Staff member : sagar Address of Staff member : pune Number_of_hours of Staff member : 5 Rate_perhour of Staff member : 300 =================================== ====Details of FullTime Staff==== Name of Staff member : abhishek Address of Staff member : nagar Department of Staff member : electronic Salary of Staff member : 20000 =================================== PS D:\JavaProgram>

4. Exception Handling

a. Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 12 15 2015, 31 6 1990, 29 2 2001

Program :

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class InvalidDateException extends Exception {} class MyDate { int dd; int mm; int yyyy; public void accept() throws IOException,InvalidDateException { BufferedReader br = new BufferedReader(new InputStreamRead er(System.in)); System.out.print("Enter the dd : "); dd = Integer.parseInt(br.readLine()); System.out.print("Enter the mm : "); mm = Integer.parseInt(br.readLine()); System.out.print("Enter the yyyy : "); yyyy = Integer.parseInt(br.readLine()); int flag= 0 ; try{ if(mm<= 0 || mm> 12 ) { throw new InvalidDateException(); }else if(mm== 4 ||mm== 6 ||mm== 9 ||mm== 11 ) { if(dd>= 1 && dd<= 30 ) { flag= 1 ; }else{ throw new InvalidDateException(); } }else if(mm== 1 ||mm== 3 ||mm== 5 ||mm== 7 ||mm== 8 ||mm== 10 ||mm== 12 )

Output :

PS D:\JavaProgram> cd "d:\JavaProgram" ; if ($?) { javac Ass4SetB1.java } ; if ($?) { java Ass4SetB1 } Enter the dd : 25 Enter the mm : 12 Enter the yyyy : 2000 Given date is : 25/12/ PS D:\JavaProgram> cd "d:\JavaProgram" ; if ($?) { javac Ass4SetB1.java } ; if ($?) { java Ass4SetB1 } Enter the dd : 32 Enter the mm : 12 Enter the yyyy : 2000 Invalid Date!!!! PS D:\JavaProgram>

5. I/O and File Handling

a. Write a program to accept a string as command line argument and check whether it is a file or directory. If it is a directory, list the contents of the directory, count how many files the directory has and delete all files in that directory having extension .txt. (Ask the user if the files have to be deleted). If it is a file, display all information about the file (path, size, attributes etc).

program :

import java.io.File; import java.util.Scanner; public class Ass5SetA { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String string = args[ 0 ];

File f = new File(string); if(f.isDirectory()) { String s[]= f.list(); System.out.print("Total number of files are : "+s.length); System.out.println("======================== =========="); for(int i= 0 ;i<s.length;i++) { System.out.println(s[i]); } System.out.println("======================== =========="); for(int i= 0 ;i<s.length;i++) { if(s[i].endsWith(".txt")) { System.out.print("Can you de lete "+s[i]+" file Y(1)/N(0) : "); int op = sc.nextInt(); if(op== 1 ) { File f1 = new File(s [i]); f1.delete(); System.out.println(" Deleted successfull"); } } } } if(f.isFile()) { System.out.print("\nThe path of the file is : "+f.getPath()); System.out.print("\nThe absolute path of the file is : "+f.getAbsolutePath()); System.out.print("\nThe lenght of file is : "+f.length()+"bytes "); System.out.print("\nLast modified date : "+f .lastModified()); } } }