Use Getters / Setters Only When They Are Needed?

May 30, 2015

Should we always use getters/setters, e.g. even in something like

Java Code:

class Foo {
private static int foo;
public int getFoo() {
return foo;
}
public void setFoo(int fig) {
foo = fig;
}
} mh_sh_highlight_all('java');

Or should we use the only when there is a good reason to use them, e.g. validation for a setter or may be computing a value to return from some variables etc. ?

View Replies


ADVERTISEMENT

Setters And Getters In Java Program - Dealing With Circles

Oct 29, 2014

You should write a class that represents a circle object and includes the following:

1. Private class variables that store the radius and centre coordinates of the object.
2. Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre coordinates supplied.
3. Public instance methods that allow the radius and centre coordinates to be set and retrieved (often known as set/get methods).
4. Public instance methods that return the circumference and area of the circle.
5. A public class method that tests if two circle objects overlap or not

Here is my code:

import java.lang.Math;
public class Circle {
private double xCentre, yCentre, Radius;
// constructors
public Circle() {
xCentre = 0.0;
yCentre = 0.0;
Radius = 1.0;

[Code] ....

The second program just has to demonstrate each aspect addressed in the brief I pasted at the top.s of what else I can do.

This is as far as I got during the 3 hour lab session I had and both compiled fine but when running just displayed all the text eg. "Circumference of first circle is ", but didn't display any numeric values. I don't have the facilities to actually run the program unless I'm in the computer lab, I have a short opportunity to go in tomorrow but that will be the last so I'm doubtful that I'll get it fully working in time.

The problem is that when this code runs it doesn't display any numerical values, ie nothing is being passed between the two programs.

View Replies View Related

Swing/AWT/SWT :: GUI - Create User Interface That Interacts With Setters And Getters Of Some Classes

Apr 14, 2014

I am new a creating GUIs and am not quite sure how to correctly make one. I have done the inheritance parts, and created two extra appliances: a washer and dryer. Now Creating the GUI ....

Here are the instructions to my project.

Introduction to GUIs (+ some inheritance)

For this assignment, you are going to create a user interface that interacts with the setters and getters of some classes that you will create.

First, create an abstract class called Appliance. This abstract class should have two attributes (dealing with household appliances) and two abstract methods called turnOn() and turnOff(). These methods should return void.

Then, create two subclasses of Appliance that represent household appliances (like a Refrigerator or Stove ((don't use those!))). These subclasses should have two attributes that are specific to the various appliance. Each subclass should implement the turnOn() and turnOff() methods. These methods should print to the command line some information about the appliance as it turns on and off.

Now, the fun part! Create a GUI interface!

Your window should have two panels: one for each appliance subclass. Each panel should have 4 textboxes (with appropriate labels) to receive/display information that correspond to the 4 attributes (2 from Appliance and 2 from the subclass) for each subclass.You also need 2 buttons on each panel: A Get button and a Set button.

When the Get button is pressed, the text boxes should be filled with the information from the instantiated object of the appropriate subclass. When the Set button is pressed, the object should then contain the information contained that the user has altered.

In your main method, you should create an object of each subclass, and prefill it with information (either using the constructor or the setters), then display your GUI. You should now be able to get and set the information for your objects from the GUI.

At least one of your attributes for each subclass should be numeric

Note that you will need to handle incorrectly formatted input (You can use exception handling to do this if you want to. Wrapper classes also will work)

If there is text in the boxes when the "Get" button is pressed, it should be overwritten by what is in the object. Remember that these two panels should both be on screen at the same time.

You don't need 2 different windows, one window: 2 panels.

View Replies View Related

Java Getters - How To Reuse Them

Jul 16, 2014

I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel).

public class GamePanels {
JPanel begin, middle, end;
public void begin(){
begin = new JPanel();
begin.setBackground(Color.MAGENTA);

[Code] ....

And this is a snippet from the class which is using this class:

newButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
GamePanels g = new GamePanels();
g.begin();
container.add("Begin", g.getPanel());
cl.show(container, "Begin");
}
});

My question is, instead of creating a huge number of getters and then remembering which getter I need, is there a way to re-use the same one? Like maybe I can add a parameter?

View Replies View Related

Why Interfaces Are Needed In Java

Dec 5, 2014

why interfaces are needed in Java,Now you saw what a class must do to avail itself of the s... - justpaste.it (if I paste the quote here, I get the "Page not found" error after posting -.^)

the first fragment reads that the compiler must be sure that a method exits at a compile time, whereas the second fragment denies it - if a[i] doesn't have the specified compareTo method, a JVM simply throws an exception.

View Replies View Related

Why Array List Is Needed

Apr 28, 2015

method called []getLetterGrades but the only hint My professor told me was that I needed to declare another array list for this method and he wouldnt tell me anything else so bummer. But I don't understand why if what we are returning is a char. It would make sense to return an array list of char to get letter grade. Which is what i did but since the function is a char, the array list character wont work as a return.Primarily i would like to know the type that is needed. I just want an explanation for an array list in this method and how it would serve in this method.

import java.io.File;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

[code]....

View Replies View Related

Applets :: Can Not Find Needed Classes

Feb 25, 2014

I have a HTML page with an applet inside it.I have the applet class and all other needed classes in a jar file in jre/lib/ext folder (I'm on windows). But when I open the HTML file in a browser it gives me ClassNotFound exception..The HTML file is this:

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>
DPI 3270 Printer Session
</TITLE>
</HEAD>
<BODY topmargin="0" leftmargin="0">
<SCRIPT SRC="HODVersion.js" LANGUAGE="JAVASCRIPT"></SCRIPT>
<P>

[code]....

I have this HostOnDemand class in its package in a jar file in jre/lib/ext but it can't be found.Since the applet is way too old, I'm using jre1.4.2 and netscape.

View Replies View Related

How To Find Needed Info From Java API Documentation

May 13, 2015

Let's say I'd like to print out something as a prompt without end with a new line char, so the standard System.out.println() is not an option and I'd like to find something else inplace of println().

Where to find that? I googled about that and learned printf() is the one I want, but how to get it from java api documentation.

View Replies View Related

Libraries Needed For IReport Is Added In Lib Folder

Oct 26, 2010

I have a .jar that is installed in other computer(outside netbeans). Everything works fine except with report. I am sure the libraries needed for iReport is added in my lib folder but I receive this error message when I to load the report using the program.I use command prompt to see the error:

Java Code:

java -jar projCosting.jar mh_sh_highlight_all('java');
And here is the error message:
Java Code: net.sf.jasperreports.engine.JRException: Error compiling report java
source files : [b]D:CostingRptCosting_1288072391310_955208.java[/b]
at net.sf.jasperreports.engine.design.JRJavacCompiler.compileClasses

[code]....

I have included the library in lib folder but do you think I have to install the iReport application?

View Replies View Related

Why Is HashCode Needed For Checking Duplicates In HashMap And Set

Jul 11, 2014

The keys in a HashMap and the values in a Set must all be unique, but this can be circumvented when using custom objects in a HashMap and Set, because the compiler has no way to determine if the objects are equal or not, as shown in the example below:

Java Code:

import java.util.LinkedHashMap;
import java.util.Map;
public class HashCodeEquals {
public void run(){
Person p1 = new Person(1, "John");
Person p2 = new Person(2, "Matt");
Person p3 = new Person(1, "John");

[code]....

Obviously the equals method is needed because that compares the two objects. But why is the hashCode method needed?

View Replies View Related

Why Keyword Is Needed Infront Of Abstract Class Methods

Mar 7, 2014

I have a simple classes here one is interface and another one is abstract class when i try to compile them abstract class is givving compilation error.

public interface MyInterface{
public void getName();
public void getName(String s);
}
public class HelloWorld{}
abstract class SampleClass{

[code]....

View Replies View Related

Program Will Require User To Enter Needed Values One After Another

Feb 21, 2015

I have to meet certain requirements for this one program, and one of them is as follow: The program will require the user to enter the needed values one after another. If any of the entered values is invalid, then the program will detect that and go into a loop requiring the user to enter a valid value. The program must keep track of the total number of invalid values that the user has entered.

I know how to set a restriction and create a loop to keep asking the same question until it is satisfactory ( in this case I used a while loop) but I am at loss on how I should keep track of the total number of invalid values. I know I can use an increment but how do I set a variable for when the program meets an wrong input from the user.

View Replies View Related







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