0% found this document useful (0 votes)
20 views

Java Lab PGM

The document contains three Java programs: the first demonstrates importing classes from a user-defined package and creating packages; the second demonstrates different types of constructors; the third demonstrates String class methods.

Uploaded by

shivani100coding
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Lab PGM

The document contains three Java programs: the first demonstrates importing classes from a user-defined package and creating packages; the second demonstrates different types of constructors; the third demonstrates String class methods.

Uploaded by

shivani100coding
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a JAVA program to implement the concept of importing classes

from User defined package and creating packages.

package figure;

public class square{


float side;
float area;

public square(float a)
{
side=a;
}

public void calarea(){


area=side*side;
System.out.println(“Area of square:”+area);
}
}

import figure.square;
public class A{
public static void main(String args[])
{
square s1=new square(20);
s1.calarea();
}
}
Write a JAVA program to demonstrate and implement types of
constructors.

class Student
{
int id;
String name;
Student()
{
System.out.println(“This is a smallest constructor”);
}
Student(int i,String n)
{
id=i;
name=n;
}
public static void main(String args[])
{

Student s=new Student();


System.out.println(“\n Default constructor values:”);

Student s=new Student(12,”Ram”);


System.out.println(“\n Parameterized constructor values:”);
System.out.println(“\n student id:” +s.id +”\n student name:” +s.name);
}}
Write a JAVA program to demonstrate String class and its methods.

public class StringOpr{


public static void main(String args[])
{
String s1=”Software Developer”;
String s2=new String(“Software Developer”);
System.out.println(“The original string is:”+s1);
System.out.println(“The original string is:”+s2);
System.out.println(“String converted to UPPER CASE:”+s1.toUpperCase());
System.out.println(“String converted to LOWER CASE:”+s1.toLowerCase());
System.out.println(“The first occurance of ‘a’ is”+s1.indexOf(‘a’));
System.out.println(“The last occurance of ‘e’ is”+s1.lastIndexOf(‘e’));
System.out.println(s1+”==”+s2+”!”+s1.equals(s2));
System.out.println(“The Character at position 5 is”+s1.charAt(5));
System.out.println(“The Substring extracted from position 5 to 11
is”+s1.subString(5,11));
System.out.println(“The no of Characters in”+s1+”is:” +s1.length());
}
}
Output:
The original string is: Software Developer
The original string is: Software Developer
String converted to UPPER CASE:SOFTWARE DEVELOPER
String converted to LOWER CASE: Software Developer
The first occurance of ‘a’ is 5
The first occurance of ‘e’ is 16
Software Developer == Software Developer ! true
The Character at position 5 is a
The Substring extracted from position 5 to 11 is are De
The no of Characters in Software Developer:18

You might also like