Name : Anamika Chauhan
PRN : 246109049
Batch : SB3
Expirement 2
Aim: To write a code using Class and Object and paste its output.
Theory:
Class
A class is a blueprint or template used to create objects.
It defines data members (variables) and member functions (methods) but does not occupy
memory until an object is created.
Example:
Student, Account, Car
Object
An object is an instance of a class.
It represents a real-world entity and occupies memory.
Objects can access the data and methods defined in the class.
Example:
Student s1 = new Student();
Code 1:
import [Link];
class InputHelper {
int a, b, choice;
void takeInput() {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
a = [Link]();
[Link]("Enter second number: ");
b = [Link]();
[Link]("Enter 1 to Add, 2 to Sub, 3 to Mul, 4 to Div: ");
choice = [Link]();
public class classObj {
public static void main(String[] args) {
InputHelper obj = new InputHelper();
[Link]();
switch ([Link]) {
case 1:
[Link]("Addition is: " + (obj.a + obj.b));
break;
case 2:
[Link]("Subtraction is: " + (obj.a - obj.b));
break;
case 3:
[Link]("Multiplication is: " + (obj.a * obj.b));
break;
case 4:
if (obj.b != 0)
[Link]("Division is: " + (obj.a / obj.b));
else
[Link]("Division by zero not allowed");
break;
default:
[Link]("Invalid choice");
output
Code 2:
import [Link];
class StudentInput {
String name, prn;
int m1, m2, m3;
int total;
double percentage;
void takeInput() {
Scanner sc = new Scanner([Link]);
[Link]("Enter Student Name: ");
name = [Link]();
[Link]("Enter PRN: ");
prn = [Link]();
[Link]("Enter marks of English: ");
m1 = [Link]();
[Link]("Enter marks of Mathematics: ");
m2 = [Link]();
[Link]("Enter marks of Science: ");
m3 = [Link]();
}
void calculate() {
total = m1 + m2 + m3;
percentage = total / 3.0;
void display() {
[Link]("\n----- GRADE CARD -----");
[Link]("Name : " + name);
[Link]("PRN : " + prn);
[Link]("Total Marks: " + total);
[Link]("Percentage : " + percentage);
if (percentage >= 90)
[Link]("Grade : A+");
else if (percentage >= 80)
[Link]("Grade : A");
else if (percentage >= 70)
[Link]("Grade : B");
else if (percentage >= 60)
[Link]("Grade : C");
else if (percentage >= 50)
[Link]("Grade : D");
else
[Link]("Grade : F (Fail)");
public class classObj {
public static void main(String[] args) {
StudentInput obj = new StudentInput(); // Object creation
[Link]();
[Link]();
[Link]();
output