How Does Encapsulation Improve Maintainability

Jan 29, 2014

I understand making things private and using getters and setters to prevent corrupt data/valdiation. I am having trouble picturing a scenario where I can change my code without breaking others. What can I change without breaking existing code? One example I can think of is I can change an instance variable name since the getter for it will return it regardless. I am looking for a more vivid example...

View Replies


ADVERTISEMENT

Does Encapsulation Mean That Variables By Default Need To Be Private

Apr 9, 2015

I just recently started learning about encapsulation, how to set variables private and only accessible to other classes through setters() and getters(). I've seen a couple of programming examples, perhaps not enough but some from here, and I sort of built the assumption that by default all variables need to be private. to make things more clear, here's a card dealer I made which simply

1- generates a fulll deck of 52 cards
2- lets user decide how many players, with 5 as Max number allowed as each player is dealt 10 cards.
3- deal cards

I approached this by making A deck , card , player and game class

import java.util.ArrayList;
public class Deck {
//an Object of this Class would generate a full deck ie an ArrayList of 52 Cards
private String[] suits={"Spades","Diamond","Clubs","Hearts"};
private int[] number={1,2,3,4,5,6,7,8,9,10,11,12,13};
ArrayList<Cards> deck= new ArrayList<Cards>();

[code]....

I can understand why for example Deck class's suit and number arrays are set to private , as they only need to be accessed by this class only. however, both the Deck class's deck arraylist and the Player class arraylist are not private, as I need to transfer these values from one to the other and it felt to me that setting them to private would make it more difficult to deal with them, but what I did instead is to set the Game class dealCard(), which is the only method that have access to them as private. does this achieve the same effect or do I need to set both of these arrayList to private?a follow up question, this is more related to the actual card dealer program, in this code

private void dealCards(){

for(int x = 0 ; x < playerCount ; x++){
for(int y = 0 ; y < 10; y++){
playerList.get(x).pile.add(deck.deck.get(0));
deck.deck.remove(0);
}
}
}

is there an API in ArrayList class that moves(adds to receiver and remove from giver) element across ArrayLists?

View Replies View Related

Encapsulation Hide Private Variable

Sep 10, 2014

As below code showing that you cannot directly access the private variable as i understood,  
 
public class EncapsulationDemo{   private int ssn;  
private String empName;   private int empAge;   //Getter and Setter methods  
public int getEmpSSN(){   return ssn;   }  
public String getEmpName(){   return empName;  

[Code] .....

View Replies View Related

Encapsulation - Protected Attributes Should Be Visible To Child Classes

Jun 7, 2013

I found the following inheritance and encapsulation issue . Suppose you have a parent class with a non-static protected attribute.

package package1;
public class Parent{
protected int a = 10; // this is the non-static protected attribute in question
public static void main(String args[]){
// whatever logic
}// end Parent class
}// end main()

Now suppose you have a child class in another package and you have imported in the parent class.

package package2;
import package1.Parent;
public class Child extends Parent{
public static void main(String[] args){
Parent p = new Parent();
Child c = new Child();

System.out.println(p.a); //should print out 10 BUT DOES NOT
System.out.println(c.a); //should print out 10
}// end main()
}// end Child class

My observation is that p.a produces an error even though, to the best of my knowledge, it should not. I believe the statement "System.out.println(p.a);" should print out a 10.

Am I misunderstanding something about inheritance and encapsulation?

View Replies View Related

How To Improve Battleship Game Java

Dec 3, 2014

I have an battleship program that I would like to improve in performance. I know that using certain algorithms it can be improve however I am beginner in Java. I would like any recommendations and or hint in how to improve this game. The are 5 ships per game and the computers plays 10000. I just want to sink all ships in as many shots as possible.

Main class

package solution;
import battleship.BattleShip;
import java.awt.Point;
import java.util.List;

[Code] .....

View Replies View Related

How To Improve JSP Page Load Performance

Feb 18, 2015

How to improve jsp page load performance and i have use sessions in jsp page.i tried using with include tag rather than using jsp:include. also

View Replies View Related

JavaFX 2.0 :: How To Improve Application Startup

Aug 26, 2014

I have a UI that uses fx:include to include a handful of nodes in a StackPane.  So far I have less than 10 panes and I can already notice a delay of ~3 seconds (on an older machine) when the initial scene is built.  It's especially noticeable because I'm using a pre-loader with a progress bar.  The progress bar runs smoothly until the pre-loader calls start() on my application.  After that, the scene is built on the application thread, so the progress bar doesn't get any more updates.  It looks like the progress bar freezes until the main scene is built and shown.
 
I was hoping I could build the main scene on the JavaFX launcher thread, but that doesn't work.  I tried it and, not surprisingly, get an exception for not being on the application thread.  What are the options, if any, for making an application's start up feel a bit smoother?

View Replies View Related

3 Different Files Using Encapsulation (Data Hiding) - Object Creation Error

Mar 21, 2015

I have my code in 3 different files using encapsulation (Data hiding) and i have 1 problem at the very end of my code in my if and else statement (very bottom) when trying to call the classes from the other 2 documents. I will put the code in 1st document to 3rd document.

// FIRST DOCUMENT
public class CollegeCourse { //class name
//variables
String deptName;
int courseNum;
int credits = 3;
double fee;

[Code] ....

UPDATE: error message is

UseCourse.java:24: error: cannot find symbol
LabCourse lc = new LabCourse(department, course, Credits);
^
symbol: variable department
location: class UseCourse
UseCourse.java:24: error: cannot find symbol
LabCourse lc = new LabCourse(department, course, Credits);

[Code] ....

4 errors

View Replies View Related

Improve Code To Output A Word With Most Frequent Letter?

Jan 31, 2015

The code is supposed to output a word from a sentence(from a sample file) with most frequently occurring letter in that word.

Example input: "Therefore, it is this word."

output: therefore

It currently uses 3 loops nested loops, is there a way to improve this?

public static String findWord(String file) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file));
String text, correctWord = null;
text = in.readLine();
StringTokenizer tokens = new StringTokenizer(text.toLowerCase(), " ,.?"); //split string into tokens of words only
int count = 0, numLetter = 0;

[code]....

View Replies View Related

How To Improve File Reading Efficiency And Its Data Insertion In Java

Feb 8, 2014

We have an autosys job running in our production on daily basis. It calls a shell script which in turn calls a java servlet. This servlet reads these files and inserts the data into two different tables and then does some processing. Java version is 1.6 & application server is WAS7 and database is oracel-11g.

We get several issues with this process like it takes time, goes out of memory etc etc. Below are the details of the way we have coded this process.

1. When we read the file using BufferedReader, do we really get a lot of strings created in the memory as returned by readLine() method of BufferedReader? These files contain 4-5Lacs of line. All the records are separated by newline character. Is there a better way to read files in java to achieve efficiency? I couldnt find any provided the fact that all the record lines in the file are of variable length.

2. When we insert the data then we are doing a batch process with statement/prepared statement. We are making one batch containing all the records of the file. Does it really matter to break the batch size to have better performance?

3. If the tables has no indexes defined nor any other constraints and all the columns are VARCHAR type, then which operation will be faster:- inserting a new row or updating an existing row based upon some matching condition?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved