Question

JAVA Write code to define a Customer class, which has id and name attributes of String...

JAVA

Write code to define a Customer class, which has id and name attributes of String data type and points attribute of integer data type, without breaking encapsulation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Encapsulation:-- Encapsulation is the Hiding concept of the data.It is used to wrap the data into a single unit so that any outer member can not access thes rights.Encapsulation is the most important feature of Object Oriented Progarming Language.Java is the one pof the pure Object Oriented Programming Language,It can Ptovide the most security of the data.

Here i have written a Customer class which has three Private attributes which can not accsess out side .It can be access only by using getter and setter method.

You have to save the Customer class as  Customer.java and Encapsulation.java then run the Programe ,You will get this out/put

Customer Name : Sushil Points:120 Customer Id:120

Programe Code:

public class Customer{

private String id;

private String name;

private int points;

public String getId() {

return id;

}

public String getName() {

return name;

}

public int getPoints() {

return points;

}

public String setId(String id) {

id=id;

}

public String setName(String name) {

name=name;

}

public String setPoints(int points) {

points=points;

}

}

public class Encapsulation{

public static void main(String args[]) {

Customer c1 = new Customer();

c1.setName("Sushil");

c1.setId("C-001");

c1.setPoints(120);

System.out.print("Customer Name : " + c1.getName() + " Age : " + c1.getPoints() + "CUstomer ID :" + c1.getId() );

}

}

Benifits of Encapsulation:--

1. A class i having total controll what it has data or field or properties

2. Data or field or properties can be only read or right .

Add a comment
Know the answer?
Add Answer to:
JAVA Write code to define a Customer class, which has id and name attributes of String...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java need to review 20) (1 point) Write the constructor for the class Customer, which...

    in java need to review 20) (1 point) Write the constructor for the class Customer, which has the name and the ID of the Customer and a reference to an Account object as its attributes. You do not need to write anything for Account class or declare the Customer class's attributes, just write the constructor.

  • Define a class Horse with two attributes, a string name and an integer numLegs. Write a...

    Define a class Horse with two attributes, a string name and an integer numLegs. Write a two parameter constructor and   a toString() method. *Write a class named Coll3 with a static method   "updateName". There are three parameters to the method: a   collection, an old name, and a new name. For every horse in the collection, if it has the same name as the parameter oldname, then   replace the horse's name with the parameter new name.   example: updateName({[X,4],[G,4],[X,3]}, X, Y) changes...

  • In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...

    In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...

  • Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides...

    Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides the following. • Two attributes: lastName and first Name of type String. • Appropriate initialization, accessors/mutator methods. 2. (6 marks) Write a Student class that is a subclass of Person class (in the previous question) which provides the following. • Two attributes: A unique student ID of type String and GPA of type float; the student ID is initialized when a Student object gets...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • Question 3 (CLOS: 3.5) Marks: 4+4 = 8 (a) Write a code in java to make...

    Question 3 (CLOS: 3.5) Marks: 4+4 = 8 (a) Write a code in java to make a class called Animal. The class has data attributes called name. Make one constructor which initializes value of name attribute (b) Make a class called Cat and make it children of Animal class. Declare a variable of the class called age. Make a constructor of this class which initializes both age and name of the class. Make a main method in the class and...

  • C++ In the code cell below, define a class named Student with an integer id and...

    C++ In the code cell below, define a class named Student with an integer id and a string name. This class should have a constructor that takes two arguments: one for the id and the other for the name. Then Create another class named CollegeStudent that publicly inherits from Student. This class should have a protected member named major. It should also have a constructor that takes three arguments: id, name, and major. This constructor should delegate initializing the id...

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT