0% found this document useful (0 votes)
10 views7 pages

Java Student Class Program Example

The document describes a Java program that defines a Student class with attributes such as USN, Name, Branch, and Phone, and includes methods to read and display student details. It also provides a main class to create multiple Student objects based on user input and print their details in a formatted manner. Additionally, sample programs demonstrating various Java concepts like array manipulation, matrix addition, and object creation are included.

Uploaded by

kjbhanushree
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Java Student Class Program Example

The document describes a Java program that defines a Student class with attributes such as USN, Name, Branch, and Phone, and includes methods to read and display student details. It also provides a main class to create multiple Student objects based on user input and print their details in a formatted manner. Additionally, sample programs demonstrating various Java concepts like array manipulation, matrix addition, and object creation are included.

Uploaded by

kjbhanushree
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab Program2: Create a Java class called Student with the following details as variables within

it. USN Name Branch Phone Write a Java program to create n Student objects and print the
USN, Name, Branch, and Phone of these objects with suitable headings.
import [Link];

class Student {

String USN, Name, Branch, Phone;

Scanner input = new Scanner([Link]);

void read() {
[Link]("Enter Student Details");
[Link]("Enter USN");
USN = [Link]();

[Link]("Enter Name");
Name = [Link]();

[Link]("Enter Branch");
Branch = [Link]();

[Link]("Enter Phone");
Phone = [Link]();
}

void display() {
[Link]("%-20s %-20s %-20s %-20s", USN, Name, Branch, Phone);
}
}

class StudentDetails {
public static void main(String[] args) {

Scanner input = new Scanner([Link]);


[Link]("Enter number of student details to be created");
int number = [Link]();

Student s[] = new Student[number];

// Read student details into array of student objects

for (int i = 0; i < number; i++) {


s[i] = new Student();
s[i].read();
}
// Display student information

[Link]("%-20s %-20s %-20s %-20s", "USN", "NAME", "BRANCH",


"PHONE");
for (int i = 0; i < number; i++) {
[Link]();
s[i].display();
}
[Link]();
}
}

Sample programs:

1. Program to create array and finding the minimum of the array with the help of the
member function by passing array as parameter using both for and for each
class Testarray{
public static void main(String args[]){

int a[]=new int[5];//declaration and instantiation


a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

int b[]={33,3,4,5};//declaration, instantiation and initialization

//printing array
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]);

[Link](" array contents of b using for each loop" );

for(int j:b)

{
[Link](j);
}
[Link]("minimum of array a is");
min(a);
[Link]("minimum of array b is");
min(b);

}
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<[Link];i++)
if(min>arr[i])
min=arr[i];

[Link](min);
}
}

2. Java program to add two matrices


public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};

//creating another matrix to store the sum of two matrices


int c[][]=new int[3][3]; //3 rows and 3 columns

//adding and printing addition of 2 matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
[Link](c[i][j]+" ");
}
[Link]();//new line
}
}}

3. Java program to create array of Objects

public class ArrayOfObjects


{
public static void main(String args[])
{
//create an array of product object
Product[] obj = new Product[5] ;
//create & initialize actual product objects using constructor
obj[0] = new Product(23907,"Dell Laptop");
obj[1] = new Product(91240,"HP 630");
obj[2] = new Product(29823,"LG OLED TV");
obj[3] = new Product(11908,"MI Note Pro Max 9");
obj[4] = new Product(43590,"Kingston USB");
//display the product object data
[Link]("Product Object 1:");
obj[0].display();
[Link]("Product Object 2:");
obj[1].display();
[Link]("Product Object 3:");
obj[2].display();
[Link]("Product Object 4:");
obj[3].display();
[Link]("Product Object 5:");
obj[4].display();
}
}
//Product class with product Id and product name as attributes
class Product
{
int pro_Id;
String pro_name;
//Product class constructor
Product(int pid, String n)
{
pro_Id = pid;
pro_name = n;
}
public void display()
{
[Link]("Product Id = "+pro_Id + " " + " Product Name = "+pro_name);
[Link]();
}
}
4. Program to create simple object within the same class

public class CreateObjectExample1


{
static void display()
{
[Link](“member function called without creating object withthe use of static
keyword”);
}
void show()
{
[Link]("object created and used");
}
public static void main(String[] args)
{
//creating an object using new keyword
CreateObjectExample1 obj = new CreateObjectExample1();
//invoking method using the object
[Link]();
[Link](); //calling member function directly using class name as the
method is declared as static
//static members of the class can be called using the class name directly

}
}

5. Program to create object outside the class


class Test
{
void show()
{
[Link]("Welcome to javaTpoint");
}
}
public class Testarray //main class to be declared as public
{

public static void main(String[] args)


{
//creating an object using new keyword
Test obj = new Test();
//invoking method using the object
[Link]();
}
}

6. Program to demonstrate use of private and public variable


class A
{
private String msg="Try to access the private variable outside the class";

}
public class PrivateExample1 {
public static void main(String[] args) {
A a=new A();
[Link]([Link]);

}
}

7. Program to demonstrate use of private through class member


class A
{
private String msg="Try to access the private variable inside the class";
void display()
{
[Link](msg);
}

}
public class PrivateExample1 {
public static void main(String[] args) {
A a=new A();
[Link]();

}
}

8. program to demonstrate use of constructor


class cons{
int data;

cons()
{
[Link]("printed through constructor");

}
cons(int j)
{
data=j;
[Link]("parameterised constructor");
[Link]("value of j is "+j+" value of data is "+data);
}

public class paramcons


{

public static void main(String args[])

{
cons b=new cons();
cons c=new cons(9);
}
}

9. Write a program to print the names of students by creating a Student class. If


no name is passed while creating an object of Student class, then the name
should be "Unknown", otherwise the name should be equal to the String value
passed while creating object of Student class.

You might also like