Chapter 2 Comp
Chapter 2 Comp
The public modifier in Java is an access modifier that allows a class, method, or variable to be accessed from any other class or package. When a member is declared as public,
it can be accessed and used by any other class or object in the program.
2. If you use the private modifier before a data member, what does it mean?
When the private modifier is used before a data member in Java, it means that the member is only accessible within the same class. It cannot be accessed or modified directly
by other classes or objects. Private members are typically used to encapsulate data and provide controlled access to them through getter and setter methods.
To create a class data member in Java, you declare a variable within the class, but outside any method or constructor. This creates a member variable that is associated with
Class members can be initialized within the class using an initializer block or through assignment within the declaration itself
7. Which return type must be used if the method does not return any value?
The "static" keyword is used to make a variable a class variable (also called a static variable).
class declaration is used to define a blueprint for creating objects. It specifies the properties and behaviors that objects of the class will have.The basic syntax for a class
A method declaration is used to define a behavior or action that can be performed by an object of a class. The basic syntax for a method declaration is: returntype
Access modifiers in Java are keywords that determine the accessibility or visibility of classes, variables, and methods within a program. Java provides four types of access
modifiers:
public: The public access modifier allows unrestricted access to the class, variable, or method from anywhere in the program.
private: The private access modifier restricts access to the class, variable, or method only within the same class. It is not accessible from other classes.
protected: The protected access modifier allows access to the class, variable, or method within the same class, subclasses, and classes in the same package.
default: If no access modifier is specified, the default access modifier is applied. It allows access to the class, variable, or method within the same package only.
3. How can you access data members and member methods through an object?
To access data members and member methods through an object in Java, you use the dot (.) operator.the syntax is objectName.dataMember;/ .methodName();
4. When will you use instance variables and class variables in a class?
Instance variables are declared within a class but outside any method, constructor, or block. They are associated with each instance (object) of the class and have separate
Class variables are declared with the static keyword within a class but outside any method, constructor, or block. They are shared among all instances of a class and have only
one copy in memory. Class variables are associated with the class itself
To access class variables outside the class, you use the name of the class followed by the dot operator and the name of the class variable. Since class variables are shared
among all instances of a class, you can access them without creating an object of the class.
6. What will you write in the parameter list of a method, if the method actually does not take any parameters?
If a method does not take any parameters, the parameter list is left empty. It is represented by a pair of empty parentheses”()”.
int totalamt: To store the amount to be paid after updating the original amount
Member methods:
void accept () - To take input for name, coach, mobile number and amount.
void update() - To update the amount as per the coach selected(extra amount to be added in the amount as follows)
First AC 700
Second AC 500
Third AC 250
Sleeper None
void display() - To display all details of a customer such as name, coach, total amount and mobile number. Write a main method to create an object of the class and all the
import java.util.Scanner;
class RailwayTicket {
String name;
String coach;
long mobno;
int amt;
int totalamt;
void accept() {
name = sc.nextLine();
coach = sc.nextLine();
mobno = sc.nextLong();
void update() {
amt=0;
System.out.println(“enter the another money if u want to add in add for another ticket”);
amt = sc.nextInt();
void display() {
System.out.println(name);
System.out.println(coach);
System.out.println(totalamt);
System.out.println(mobno);
ticket.accept();
ticket.update();
ticket.display();