First CIE QP and Scheme Java
First CIE QP and Scheme Java
First CIE QP and Scheme Java
Or
c. Write Java program to define a class called Student with the data members (USN, Name,
Branch, Phone Number) and write methods to read and display the students details with
appropriate messages.
class Student
{
String name,USN,branch,phoneno;
void insertData(String n, String u, String b, String p)
{
04
name = n;
USN=u;
branch=b;
phoneno=p;
}
void displayInformation()
{
System.out.println("Name "+name);
System.out.println("USN "+USN);
System.out.println("Branch "+branch);
System.out.println("Mobile Number "+phoneno);
}
}
class StudentInfo
{
public static void main(String args[])
{
Student s1 = new Student();
s1.insertData("John","2BL22CS100","cse","9797979797");
s1.displayInformation();
}
}
If, core code is correct= 2 m, If, entre source code is correct= 4 m
Q.4.a. static
Static variables are also known as class variables. These variables are declared similarly
to instance variables. The difference is that static variables are declared using the static
keyword within a class outside of any method, constructor, or block.
Unlike instance variables, we can only have one copy of a static variable per class,
irrespective of how many objects we create.
Static variables are created at the start of program execution and destroyed
automatically when execution ends.
this keyword in Java 04
The this keyword in Java is a reference variable that refers to the current object of a
method or a constructor. The main purpose of using this keyword in Java is to remove
the confusion between class attributes and parameters that have same names.
Explanation of static with examples=2 m, Explanation of this with
examples=2 m
04
c. Develop a stack class to hold a maximum of 10 integers with suitable methods.
class Stack {
int stck[] = new int[10];
int tos;
Stack() {
tos = -1;
}
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
04
return 0;
}
else
return stck[tos--];
}
}
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
for(int i=0; i<10; i++) mystack1.push(i);
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
}
}
If, core code is correct= 2 m, If, entre source code is correct= 4 m
…