Giving the following code snippet:
Discuss the reasons of using private access specifier in some places of the code and public in other places. Additionally, discuss how can you test this code.
public class Book
{
privateStringbookName;
privateinted;
private String author;
public Book (String n, int e, String a)
{
bookName = n;
ed = e;
author = a;
}
/**
* Gets the book name.
* @return the book name.
*/
public String getBookName()
{
returnbookName;
}
/**
* Gets the book edition.
* @return the book edition.
*/
publicintgetBookEd()
{
returned;
}
/**
* Gets the book author.
* @return the book author.
*/
public String getAuthor()
{
return author;
}
/**
* Prints the Book details.
*/
public void printBookDetails()
{
System.out.println("Book Name:" + getBookName() );
System.out.println("Book edition: " + getBookEd() );
System.out.println("Book Author: " + getAuthor() );
}
}
* use MS Format
* be sure of answer
Access modifiers in java defines the scope of the members of the class . This is used for data abstraction , so that functions from outside the class cannot get access to the one inside a class .
there are basically for types of access modifiers
now since we understand the basic types of modifier lets try testing the program , i am assuming here we need to test the working of the access modifiers
1. lets first see the working of the public members , that is they can be called from outside the class
class Book
{
private String bookName;
private int ed;
private String author;
public Book (String n, int e, String a)
{
bookName = n;
ed = e;
author = a;
}
/**
* Gets the book name.
* @return the book name.
*/
public String getBookName()
{
return bookName;
}
/**
* Gets the book edition.
* @return the book edition.
*/
public int getBookEd()
{
return ed;
}
/**
* Gets the book author.
* @return the book author.
*/
public String getAuthor()
{
return author;
}
/**
* Prints the Book details.
*/
public void printBookDetails()
{
System.out.println("Book Name:" + getBookName() );
System.out.println("Book edition: " + getBookEd() );
System.out.println("Book Author: " + getAuthor() );
}
}
public class test{
/* lets test the access modifiers */
public static void main(String args[]){
/* now lets call the consructor which is public or otherwose we
wont be able to call it */
Book book_1 = new Book("50 shades of grey " , 5
,"xyz");
/*Calling the other public functions of the class
*/
String b = book_1.getBookName();
System.out.println("Book Name:" + b );
int c = book_1.getBookEd();
System.out.println("Book edition: " + c );
String d = book_1.getAuthor();
System.out.println("Book Author: " + d);
System.out.println(" ");
book_1.printBookDetails();
}
}
The output of the following program is :

and this is what we were expecting that we will be able to call the function .
2 . now for the private access modifier : we have kept some of our data private so that malicious codes cannot access or modify out data directly so lest test whether we can access those variables from outside and we are expecting a compile time error
class Book
{
private String bookName;
private int ed;
private String author;
public Book (String n, int e, String a)
{
bookName = n;
ed = e;
author = a;
}
/**
* Gets the book name.
* @return the book name.
*/
public String getBookName()
{
return bookName;
}
/**
* Gets the book edition.
* @return the book edition.
*/
public int getBookEd()
{
return ed;
}
/**
* Gets the book author.
* @return the book author.
*/
public String getAuthor()
{
return author;
}
/**
* Prints the Book details.
*/
public void printBookDetails()
{
System.out.println("Book Name:" + getBookName() );
System.out.println("Book edition: " + getBookEd() );
System.out.println("Book Author: " + getAuthor() );
}
}
public class test{
/* lets test the access modifiers */
public static void main(String args[]){
/* now lets call the consructor which is public or otherwose we
wont be able to call it */
Book book_1 = new Book("50 shades of grey " , 5
,"xyz");
book_1.printBookDetails();
/* lets try calling some private members of class and
lets see what we see on the compiler */
System.out.println(book_1.bookName);
System.out.println(book_1.ed);
System.out.println(book_1.author);
}
}
the output of this code is :

so each time we called a private member we got a compile time error
Giving the following code snippet: Discuss the reasons of using private access specifier in some places...
(JAVA NetBeans)
Write programs in java
Example 9.13~9.15
//Book.Java
public class Book {
private String title;
private String author;
private double price;
/**
* default constructor
*/
public Book() {
title = "";
author = "";
price = 0.0;
}
/**
* overloaded constructor
*
* @param newTitle the value to assign to title
* @param newAuthor the value to assign to author
* @param newPrice the value to assign to price
*/
public Book(String newTitle, String newAuthor, double newPrice)...
Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() { super(); System.out.println(“B() called”);...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() { super(); System.out.println(“B() called”); } public...
DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...
departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore { private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee; } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...
Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LibraryCard { private String id; private String cardholderName; ...
what is wrong with the following code? public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random(); public static String getAnswer(int category, int answer) { if (category >= 74) { if (answer == 0) { return "As I see it, yes." ; } else if (answer == 1) { return "Signs point to yes." ; } else if (answer == 2) { return "Outlook good." ; } else if...
Question 1 Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } public E getData() { . . . } } What will result from executing the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); String b = box.getData(); A. run-time error B. compiler warning C. no error D. compiler error Question 2 What is used as a...
JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...