1. Create a Java program to access the java.util.Date class through import keyword and print the current date of the system.
import java.util.Date;
class A{
public static void main(String a[])
{
Date obj = new Date();
System.out.println(obj);
}
}
Output
2. Create a Java program to access the java.util.Date class through qualified way and print the current date of the system.
/* Create a Java program to access the java.util.Date class through qualified way and print the current date of the system.*/
public class packages2_7024 {
public static void main(String[] args) {
java.util.Date currentDate = new java.util.Date();
System.out.println("Current Date: " + currentDate);
}
}
Output
3. Create a Java program to access the java.util.Date class through import keyword and print the current date of the system but inherit the Date class.
import java.util.Date;
public class CustomDate {
private Date date;
public CustomDate() {
date = new Date();
}
public Date getCurrentDate() {
return date;
}
public static void main(String[] args) {
CustomDate customDate = new CustomDate();
Date currentDate = customDate.getCurrentDate();
System.out.println("Current Date: " + currentDate);
}
}
Output
4. Create a Java program to access the java.util.Date class through qualified way and print the current date of the system but inherit the Date class.
class Date {
public static void main(String args[]){
java.util.Date date=new java.util.Date();
System.out.println(date);
}
}
Output
5. Create a Java program to create a package with multiple default classes.
package PK1;
class Class1 {
public void printMessage() {
System.out.println("This is the first class in the first package.");
}
public static void main(String[] args) {
new Class1().printMessage();
}
}
class Class2 {
public void printMessage() {
System.out.println("This is the second class in the second package.");
}
public static void main(String[] args) {
new Class2().printMessage();
}
}
class Class3 {
public void printMessage() {
System.out.println("This is the third class in the third package.");
}
public static void main(String[] args) {
new Class3().printMessage();
}
}
Output
6. Create a Java program to create a package with 4 public classes, create the object in another file and call the function created in those classes.
MyPackage/Class1.java
package MyPackage;
public class Class1 {
public void function1() {
System.out.println("Function 1 from Class1");
}
}
MyPackage/Class2.java
package MyPackage;
public class Class2 {
public void function2() {
System.out.println("Function 2 from Class2");
}
}
MyPackage/Class3.java
package MyPackage;
public class Class3 {
public void function3() {
System.out.println("Function 3 from Class3");
}
}
MyPackage/Class4.java
package MyPackage;
public class Class4 {
public void function4() {
System.out.println("Function 4 from Class4");
}
}
/MainApp.java
import MyPackage.Class1;
import MyPackage.Class2;
import MyPackage.Class3;
import MyPackage.Class4;
class Packages_6 {
public static void main(String[] args) {
Class1 obj1 = new Class1();
Class2 obj2 = new Class2();
Class3 obj3 = new Class3();
Class4 obj4 = new Class4();
obj1.function1();
obj2.function2();
obj3.function3();
obj4.function4();
}
}
Output
8. Create a Java program to create a package with public class and protected members to be accessed in another class.
MyPackage/MyClass.java
package MyPackage;
public class MyClass {
protected int protectedField;
protected void protectedMethod() {
System.out.println("Protected method in MyClass");
}
}
MyPackage/MyExtendedClass.java
package MyPackage;
public class MyExtendedClass extends MyClass {
public void accessProtectedMembers() {
protectedField = 42; // Access the protected field
protectedMethod(); // Access the protected method
System.out.println("Value of protectedField: " + protectedField);
}
}
MainApp.java
package MyPackage;
class Packages_8 {
public static void main(String[] args) {
MyExtendedClass extendedObj = new MyExtendedClass();
extendedObj.accessProtectedMembers();
}
}
Output