Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 10, Problem 1SA
Explanation of Solution
Encapsulation:
- • It combines the data and code into a single object.
- • It helps in restricting access to the methods.
- • This helps in preventing the data from being modified due to accident.
- • The object’s variable is not directly accessible.
- • Underscore helps in preventing the data from being accessed directly.
The encapsulation rules in python can be summarized as follows:
Public method | It is accessible from anywhere. |
Public variables | It can be accessed from anywhere. |
Private method | It can be accessed only in their own class. |
Private method | It is accessible only in their class or method. |
- • It protects the data from being tampered. By declaring the data field as private, direct modifications of properties are prevented.
- • It makes the class easy to maintain and reduces the errors...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In your own words, what is encapsulation?
Briefly explain the concept of Encapsulation.
Also explain how Encapsulation can be achieved.
Java - Encapsulation
A rectangle can be formed given two points, the top left point and the bottom right point. Assuming that the top left corner of the console is point (0, 0), the bottom right corner of the console is point (MAX, MAX) and given two points (all “x” and “y” coordinates are positive), you should be able to draw the rectangle in the correct location, determine if it is a square or a rectangle, and compute for its area, perimeter and center point. To be able to do this, you should create a class Point (that has an x-coordinate and a y-coordinate). Also, create another class called Rectangle. The Rectangle should have 2 points, the top left and the bottom right. You should also implement the following methods for the Rectangle:
display() - draws the rectangle on the console based on the samplearea() - computes and returns the area of a given rectangleperimeter() - computes and returns the perimeter of a given rectanglecenterPoint() - computes and returns the center point…
Chapter 10 Solutions
Starting Out with Python (4th Edition)
Ch. 10.1 - What is an object?Ch. 10.1 - Prob. 2CPCh. 10.1 - Why is an object's internal data usually hidden...Ch. 10.1 - What are public methods? What are private methods?Ch. 10.2 - You hear someone make the following comment: "A...Ch. 10.2 - In this chapter, we use the metaphor of a cookie...Ch. 10.2 - What is the purpose of the _ _init_ _ method? When...Ch. 10.2 - Prob. 8CPCh. 10.2 - In a Python class, how do you hide an attribute...Ch. 10.2 - What is the purpose of the _ _str_ _ method?
Ch. 10.2 - Prob. 11CPCh. 10.3 - What is an instance attribute?Ch. 10.3 - Prob. 13CPCh. 10.3 - What is an accessor method? What is a mutator...Ch. 10.4 - Prob. 15CPCh. 10.4 - Prob. 16CPCh. 10.4 - When designing an object-oriented application, who...Ch. 10.4 - How do you identify the potential classes in a...Ch. 10.4 - What are a classs responsibilities?Ch. 10.4 - What two question should you ask to determine a...Ch. 10.4 - Will all of a class's action always be directly...Ch. 10 - The _______ programming practice is centered on...Ch. 10 - The ___________ programming practice is centered...Ch. 10 - A(n) _____ is a component of a class that...Ch. 10 - Prob. 4MCCh. 10 - By doing this, you can hide a classs attribute...Ch. 10 - Prob. 6MCCh. 10 - A(n) ________ method stores a value in a data...Ch. 10 - Prob. 8MCCh. 10 - If a class has a method named _ _str_ _ , which of...Ch. 10 - A set of standard diagrams for graphically...Ch. 10 - In one approach to identifying the classes in a...Ch. 10 - Prob. 12MCCh. 10 - The practice of procedural programming is centered...Ch. 10 - Object reusability has been a factor in the...Ch. 10 - It is a common practice in object-oriented...Ch. 10 - Prob. 4TFCh. 10 - Starting an attribute name with two underscores...Ch. 10 - You cannot directly call the _ _ str _ _ method.Ch. 10 - One way to find the classes needed for an...Ch. 10 - Prob. 1SACh. 10 - Why should an object's data attributes be hidden...Ch. 10 - What is the difference between a class and an...Ch. 10 - The following statement calls an object's method....Ch. 10 - Prob. 5SACh. 10 - In a Python class, how do you hide an attribute...Ch. 10 - Prob. 7SACh. 10 - Suppose my_car is the name of a variable that...Ch. 10 - Prob. 2AWCh. 10 - Look at the following description of a problem...Ch. 10 - Pet Class The Pet class Write a class named Pet,...Ch. 10 - Car Class Write a class named Car that has the...Ch. 10 - Personal Information Class Design a class that...Ch. 10 - Employee Class Write a class named Employee that...Ch. 10 - RetailItem Class Write a class named RetailItem...Ch. 10 - Patient Charges Write a class named Patient that...Ch. 10 - Employee Management System This exercise assumes...Ch. 10 - Cash Register This exercise assumes you have...Ch. 10 - Trivia Game In this programming exercise, you will...
Knowledge Booster
Similar questions
- OBJECT ORIENTED IN JAVA Example for VERSION 1: Welcome to Nick's Neon Tubing Calculator Enter the shape type (R, S, C, T, L, Q): R Enter Height and Width of Rectangle: 6.5 12 The perimeter of a 6.5 x 12 rectangle is 37.0 Enter the shape type (R, S, C, T, L, Q): C Enter Diameter: 12 The circumference of circle with diameter 12 is 37.7 Enter the shape type (R, S, C, T, L, Q): Q Shapes Needed 37.0 - rectangle, 6.5 x 12 37.7 - circle, diameter 12 --------------------------- 74.7 Total Length Thank You there is a version 2 as well, if you look at the pictures. Every person I have asked to help has either not made it object oriented or that code did not work. PLEASE help.arrow_forwardDefine the class given from the picture using javaarrow_forwardJava Constructor(String) This constructor will take a string of digits(no commas) and turn it into an UnboundedInt object (MUST BE STRING INPUT) UnboundedInt add (UnboundedInt ) A method that adds the current UnboundedInt with a passed in one. The return is a new UnboundedInt. UnboundedInt multiply (UnboundedInt ) - do this one last! A method that multiplies the current UnboundedInt with a passed in one. The return is a new UnboundedInt. void addEnd ( int ) -optional method (helpful) A method to add a new element at the end of the sequence , used for building up each higher term in a single sequence. (i.e. adding a new IntNode to the linked list) UnboundedInt clone( ) a method that returns a copy of the original structure boolean equals ( Object ) a method that returns true if linked list represents the same numerical number as the input parameter. False otherwise. Overrides method in Object class. String toString ( ) creates a string of all elements…arrow_forward
- A string of ( {[]}) is given and you have to check this valid parenthesis. Means if a braces is open then it have to close before closing of outer. You have to create a separate class named solution and an another method to check this. In javaarrow_forwardCreate the following class diagram: The print method in: - Author prints the Authors data - Page prints page no and text - Textbook prints the subject and all pages - Novel prints the genre and all pages The toString method in: - Book returns title and authors - textbook returns subject in additions to super toString - Novel genre in additions to super toString Test your implementation create objects of different types example : - Create a book of type Novel which has many pages and several authors - Create a book of type Textbook which has many pages and several authors - Access these objects polymorphically, variable of class book contains objects of its subclasses Textbook and novel - Print a hall book - Print a single page of a book - Print the book and its authorsarrow_forwardJAVA ENCAPSULATIONarrow_forward
- Shape Classes Design classes Square, RightTriangle , and Triangle that encapsulate three geometric shapes. Each class should implement a method void draw (int x, int y) that “draws" a square, a right triangle, or an equilateral triangle (a triangle with three equal sides), respectively. See Figure 2. The parameters x and y specify the relative position of the figure: y lines down and x spaces acro from the current position of the screen cursor. The instance variables of each class are: int rows , the number of rows that comprise the figure, and char character , the keyboard character used for drawing the figure. Each shape of Figure 2 consists of five rows. The drawing characters are *** for the square, %' for the right triangle, and #' for the equilateral triangle. Shape (abstract) int rows char character Shape() Shape(int x, char c) int get rows() char getCharacter() void setRows(int x) void setCharacter(int x) void draw(int x, int y) (abstract) Square() Square(int x, char ch) void…arrow_forwardComplete the following code in the blanks to add the instructions on the comments class ContactInfo { private String homeAddress; private String emailAddress; private int telephoneNumber; } public class Student { private String name; private int age; private int grade; // add an aggregation relationship between class student and Contact Info ----------------------------------------------------------------------- // add a method called study and print out “Study Hard” -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- } }arrow_forwardDifference between String and Stringbuilder reference types?arrow_forward
- : A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as showing the point, setting the coordinates of the point, printing the coordinates of the point, returning the x-coordinate, and returning the y-coordinate. Also, write a test program to test the various operations on the point.arrow_forwardUsing JAVA Part A) Design a class Message that models an e-mail message. A message has a recipient, a sender, and a message text. Support the following methods: 1) A constructor that takes the sender and recipient. 2) A method append that appends a line of text to the message body A method toString that makes the message into one long string like: “From: Harry Morgan%nTo: Rudolf Reindee%n . . . “ Write a program that uses this class to make a message and print it. Part B) Design a class Mailbox that stores e-mail messages, using the Message class in part a) Implement the following methods in the Message class. public void addMessage(Message m) // Adds Message M to the Mailbox public Message getMessage(int i) // returns the ith message from the Mailbox public void removeMessage(int i) // removes the ith message from the Mailbox public int mailBoxSize () // returns the number of messages in the Mailbox Write a program to demonstrate the functionality of the Mailbox. Add messages to…arrow_forwardTackle the problem JAVA FXML Replace the Model of the tile puzzle game with a "stub" for the Model of the concentration game. This stub only needs to specify the size of the game grid at this point (that's why it's called a stub—there's not much functionality yet.) Get the program to display a 4 x 6 blank grid. Hints:- on what to do Need to replace the Model in the starter code with a model that provides logic for the concentration game. It is recommend to keep the same basic methods as the original model, except that you should replace getSide() with getRows() and getColumns() methods. *-------------------------------* package model; import java.util.ArrayList;import java.util.Random; public class TilePuzzle{private String [ ][ ] tiles;private int side; // grid sizeprivate int emptyRow;private int emptyCol;private Random randGen;private int row;private int column;/** constructor* @param newSide grid size*/public TilePuzzle( int newSide ){randGen = new Random(); setUpGame( newSide…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT