How To Create Public Get And Setter Methods For Private Members Of The Class

Mar 23, 2015

If i have a class(lets say class name is Approval) with the following private members: String recip_id, Int accStat, String pDesc, String startDate How can i create public get and setter methods for these private members of the class?

View Replies


ADVERTISEMENT

Private Members Cannot Be Accessed In Derived Class

Jul 3, 2014

When we say derived class that means copy of base class plus subclass specific implementations. But when it comes to private members it cannot be accessible in subclass scope. Does it mean byte code generated for subclass doesn't has byte code of private members of super class ?

View Replies View Related

Month Class With Constructors And Getter / Setter Methods To Access Attributes

Jul 22, 2014

Write a class Month that represents one of the twelve months of the year. It should have three attributes for

the name of the month,

the number of days in the month, and

the birthstone.

Also add constructors and getter/setter methods to access the attributes.

You may use the following code to test your class.

Java Code:

import java.util.*;
public class Month
{
// ADD CODE HERE!!!
public static void main(String[] args)
{
Month[] months = new Month[12];

[Code] ....

So what I have added so far is (under public class month { :)

Java Code:

String monthName, monthBirthStone;
int monthDays;
public Month (String name, int days, String birthstone)
{
monthName = name;
monthBirthStone=birthstone;
monthDays=days;
} mh_sh_highlight_all('java');

So I believe that is the constructor. I still do not understand several things:

What would I need the getter and setter for?

I tested it using just the above code, and using month 1 I got:

Month@5a1cfb56

This makes sense as I obviously didn't do anything in order to get it in a String format for the array. But I do not understand this still - how would I get the constructor to output a string (to then be in the array?)

View Replies View Related

Private Var Won't Save Value Of Public Getter Function From Base C

Sep 15, 2014

I'm working on this program for a class to create objects of a commissioned employee and union employee. Everything seems to work ok, but when I run my final pay calculation, one of my getter functions will not pass the variable for the pay into a class specific variable called check. here is the code in this function.

public void finalPayCal_U() {
setweekPay();//calculates weeks pay under 40 hours, stored in getweekpay
check = getweekPay();
if(getHours() > 40){
check = check + (1.5 * (getHours() - 40) * getRateOfPay());
}
check =- Dues;
}
}

The issue lies in check = getweekPay();

I thought this was a legal move, but I can't get it to work. It doesn't come back with anything more than 0 any time I run it.

All of these functions are out of the base class, employee (this function itself lives in the derived union class)

public void setweekPay() {
if (hours <= 40) {
weekPay = hours * rateOfPay;
} else {
weekPay = 40 * rateOfPay;
}
}

When I run, this function works as it returns the value when i print it to test.

however, when I do the part that says check = getWeekPay() above, it doesn't change the check variable. The only thing I have on the check variable is the dues taken out of it at the end, so it ends up being a negative number.

I have a similar problem with the other derived class's check variable. Both classes have the same variable as private but one is check the other checkC.

View Replies View Related

Java IP Address Program - Check If It Is Public Or Private

Feb 2, 2015

I am doing an assignment for a college class. We are asked to get user input and decide if it is a valid IP address and then check what class the address is and if it is a public or private address.

So far, I can get the input, and check to see if the numbers are in a valid range. I can also display the IP address to the user. I am having an issue figuring out how to get the program to check the classes and whether they are public or private.

import javax.swing.JOptionPane;
/**
This program will take user input and calculate whether it is a valid IP address and the class that it belongs to.
*/

public class shortONE_1
{
public static void main(String[] args)
{
//Variables
final int MIN_OC = 1; //Minimum number accepted
final int MAX_OC = 255; //Maximum number accepted
final int MIN_A = 1; //Min number for class A
final int MAX_A = 127; //Max number for class A

[Code] ....

View Replies View Related

Modify And Use A GUI To Set Variables Setter Methods

Feb 24, 2012

i'm trying to modify and use a GUI to set the variables the setter methods. while the code seems valid to me and should work perfectly, i get ArrayIndexOutOfBounds and StringIndexOutOfBounds and respectively lines 111 and 134. i'm am not the original author of this code, all i want is to get it to work fine.

1package de.kugihan.dictionaryformids.dictdconv;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.OutputStreamWriter;
6

[code]....

View Replies View Related

Using Setter And Getter Methods Instead Of Altering Variables

Apr 15, 2015

i need to change my code in order to stop the member variables from being directly altered and its been suggested that i should use a setter and getter method. Ive read up about these and im still unsure at how they should be implemented into my code for my project.

import ddf.minim.*;
DragShape[] piece =new DragShape[77];
PImage puz16;
PImage frame;
PImage text;

[code]....

View Replies View Related

ImageIcon Setter And Getter Methods Not Working

Jan 3, 2015

I have been assigned with a task to have a class which has the methods setImage and getImage. These methods are meant to set the ImageIcon by using the url taken from another class and the getImage method is meant to return the ImageIcon that was set before hand. The problem is that i'm not really familiar with ImageIcon so the code in both my methods is giving out errors and i just can't figure out why. Heres the code in the class that has the setImage and getImage methods:

public class Die implements DieInterface {
private ImageIcon [] image = new ImageIcon[6]; //the number of images that would be stored in this array is 6 (six faces of the dice)
ublic Die()
{
//This puts images into the images array(the different die faces)
image = new ImageIcon[6];

[code]....

And this is where i call the methods (set and get methods) in the other class:

die1.setImage(new ImageIcon(Dice.class.getResource("face1.png")));
die1.getImage();

View Replies View Related

Fields / Getter And Setter Methods And Constructors

Mar 12, 2015

A blood clinic has hired a team of software developers to build a custom application to track patients. The clinic needs to keep a record of each patient and his or her blood data. Ultimately, they want all of the information stored in a database. As a starting point, the development team leader informs the team that the application has to have a set of core classes that represent the “real-world” entities the program needs to deal with. As a developer on the team, your job is to build a Patient class along with a BloodData class so that each Patient contains a BloodData object. This principle is known as “composition.”

Building the Framework Begin by creating a public Java class named PatientBuilder that contains a main method. Then, in the same file, create a non-public class named Patient and another named BloodType. Save the file as PatientBuilder.java. Note: If this was a real development project, you would put each class into it’s own file and put the files in the same folder. By combining them all into one file, we avoid having to submit three separate files, making it easier to keep all your work in one place.The BloodData Class This class should hold all of the attributes to hold the data the clinic needs for a given patient’s blood. Implement the following capabilities to accomplish this objective:

• Create a field to hold a blood type. The four possible blood types are O, A,B, and AB. For this project, you can simply define the field as a String.
• Create a field to hold the Rh factor. The two possible Rh factors are + and –.For this project, you can simply define the field as a String.
• Create getter and setter methods for both fields.
• Create a default constructor that sets the fields to “O” and “+”
• Create an overloaded constructor that accepts two parameters – one for a proposed blood type, and another for a proposed Rh. The constructor
should call the set methods and pass these parameter variables in to them.The Patient Class This class should hold all of the attributes to hold the data the clinic needs for a given patient’s blood. Implement the following capabilities to accomplish this objective:
• Create a field to hold an ID number along with get and set methods.
• Create a field to hold the patient’s age along with get and set methods.
• Create a field to hold the BloodData for a Patient. When declaring the field, initialize it by instantiating a BloodData object. Create get and set methods.
• Create a default constructor that sets the ID to “0”, age to 0, blood type of the BloodData object to “O”, and Rh value of the BloodData object to “+”.
• Create an overloaded constructor that accepts the following parameters: ID,age, blood type, and Rh. The constructor should call the set methods for the field associated with each parameter.The PatientBuilder Class.This class should contain the main method from which the program runs. In that method, implement the following functionality:• Ask the user for the ID, age, blood type, and Rh factor of a Patient.
• Create a Patient object, passing all of the data obtained from the user into the object.
• Write the code necessary to display the ID, age, blood type, and Rh factor of the Patient by calling the appropriate get methods of the object.

MY CODE ( which does not compile since it is wrong...)

import java.util.Scanner;
public class PatientBuilder
{
public static void main(String[] args){
String patientID;
int patientAge;
String patientRh;
String patientBlood;

[code].....

View Replies View Related

Access Getter / Setter Of Bean Class Where It Defined As Member In Another Class?

Feb 18, 2014

Class UserAssessBean{
private String username;
private int userid;
private ArrayList<ModuleBean> module;
--{get/set}--

[Code] ....

How can i access the getters/setters of module bean, when it was returned as array list in UserAssessBean?

View Replies View Related

Create Instance Of Own Triangle Class And Use One Of Its Methods

Mar 15, 2014

So in the code below I create an instance of my own triangle class and use one of its methods. The thing is I use one of my triangle classes methods in a method other the main method of my main program so I'm thinking it can't access it?

Any way here's the code for my triangle class
 
import java.util.Scanner;
public class QudratullahMommandi_Triangle_06 {
Scanner keyboard = new Scanner(System.in);
private double side1;
private double side2;
private double side3;

[Code] ....

and here's the error message

QudratullahMommandi_S_06.java:46: error: cannot find symbol
{ triangle1.outPut();
^
symbol: variable triangle1
location: class QudratullahMommandi_S_06
1 error

View Replies View Related

Create Constructor With Parameters And Methods In Same Class?

Feb 28, 2014

The one problem in my book was to create a constructor for different shirt features, which I did and ran successfully. Out of curiosity, I also added other methods to see if it would run if the parameters were different from the constructor. It keeps giving me a constructor error. So, my question is, am I able to create a class that uses a constructor with parameters and other methods without errors? I'm guessing there's no reason to since it would be wasted space since the constructor could do it but was just curious if it's possible.

Is everything from the constructor down (in the class) and Shirt.oneShirt (in the main) just a waste of time?

Here's my example:

public class Shirt//class name.
{
int collarSize;//data field.
int sleeveLength;//data field.
int pocketNumber;//data field
public final static String MATERIAL = "cotton";//final data field for material.
public Shirt(int collarSize, int sleeveLength, int pocketNumber)//start of constructor.
{

[Code]...

View Replies View Related

Create Two Instances Of Class And Test Each Of Methods

Nov 1, 2014

How do I use two constructors and I'm having trouble with using char for gender...

Write a program to test the Person class defined below. Your test program should create two instances of the class (each to test a different constructor) and test each of the methods. You are also required to illustrate the error in trying to access private data members from the client class (for clarity temporarily change the private modifier to public and test again). See screenshots below for sample output.

The screen shots are displayed as:

p1 name = Not Given Age = 0 Gender = U
p2 name = Jane Doe Age = 0 Gender = F
p1 name = John Doe Age = 25 Gender = M

and

PersonTester.jave:20: name has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
 PersonTester.jave:20: age has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
  PersonTester.jave:20: gender has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
 
3 errors

Here is the class given :

class Person {
// Data Members
private String name; // The name of this person
private int age; // The age of this person
private char gender; // The gender of this person
 
[Code] .....

View Replies View Related

Create A Class Which When Implemented Requires To Have HashMap And Methods

Oct 13, 2014

I want to make several classes which extend different objects and add additional functions to simplify them and make their purpose in my projects more narrow and make their instances easier to use. So an example, Image class which extends BufferedImage and the constructor in Image class directly loads the file without having to create it first and then have to use Try Catch and all that additional code. Now, here is where my question comes in. Can I make an class, an abstract class or something which can be IMPLEMENTED into these several classes such as the Image class, and in doing so those several classes will have to have (like unimplemented methods) a HashMap<String key, ChildClass instance_as_value>, child class being the Image class as an example.

So I would have something like public class Image extends BufferedImage implements Library, and this class, because it implements Library will have a HashMap<String key, Image value> in it or it's parent class.

View Replies View Related

Inheritance And Private Methods

Jul 6, 2014

The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)

public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();

[code]....

View Replies View Related

Importing Static Members Of A Class

May 11, 2014

When importing static members of a class. Why are they only accessible within the constructor of the calling class, and not outside of it? Here's the source code to understand my question.

package Certification;
public class ExamQuestion {
static public int marks ;
static public void print(){
System.out.println(100);

[Code] ....

View Replies View Related

Object / Methods - Private Instance Variables

Jun 30, 2014

so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...

for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.

i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?

in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer.The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?

Any simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?

class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;

[code]...

View Replies View Related

Creating A Constructor Without Private Implementations Only Methods?

Jul 14, 2014

I thought you can only create a new object using private implementations and then using a constructor to set your arguments inside the parameters of the constructor to the instance variables but how come he created an object without any private implementations and just methods inside the constructor.

import javax.swing.JFrame;
public class MyWindow extends JFrame {
public static void main(String[]args){
new MyWindow();
}
public MyWindow(){
setSize(500,500);
setVisible(true);
setTitle("MyWindow");
}
}

View Replies View Related

Two Classes - How To Use Members Of Color Class Without Using Extends

Nov 4, 2014

I have two classes in two different files.

Color.java and Light.java

The Color class:

public class Color {
private int red;
private int green;
private int blue;
public Color(){
red = 0;
green = 0;
blue = 0;
}

And i have the Light class :

public class Light {
private Color color1;
private boolean switchedon;

public Light(int red, int green, int blue){
//dont know what to write here . how can i use the members of the Color class here ? without using extends.
}
}

View Replies View Related

A Public Class In Sub-folder Is Not Getting Found By Another Class While Compilation

Feb 14, 2014

I was doing coding exercise from a book ('OCP Java SE 6 - Practice Exams' by Kathy Sierra and Bert Bates). I came to a question that told to demonstrate the difference between 'default' and 'protected' access rules by creating/making a directory structure and putting a couple of classes in different packages.

For this, I made a total of four classes, out of which, three classes are-Car, TestingCars, CarDimensions. (The fourth is not yet used in testing code till now, so, I am giving only the other three classes.) Their coding is given below.

Out of these classes, the classes- TestingCars and Car - are in a directory (say, FolderName). And, the class- CarDimensions is in FolderName's sub-folder.

The class 'CarDimensions' is public (and its components too are public). And, I am testing all the classes from the class- 'TestingCars'. But, this class (TestingCars) is not able to find the public class- 'CarDimensions' which is in its sub-folder and gives two 'Cannot find symbol' errors citing the class-CarDimensions. Also, If all three classes are put in one single directory, the programs work, without any error.

Coding:
Class TestingCars:class TestingCars {
public static void main(String[] args) {
Car c = new Car();
c.setType("FourWheeler");

[Code]....

I could not find why the public class- CarDimensions- is not getting found by the TestingCars class.

View Replies View Related

Implement Equality And HashCode Method If Class Has Reference Type Members?

Jan 16, 2015

I am trying to implement the following example to override the equality and hashCode method if the class has reference type member. I do get the expected result "true" for equal and "false" for non-equal objects. But the print statement in the Circle's equal method is not executed when the objects values are not equal. I don't know what i am missing, though i get the equality result "false" as expected for non equal objects.

class Point{
private int x, y;
Point (int x, int y) {
this.x =x;
this.y = y;

[code]....

View Replies View Related

Pass Private Final Class Object To Another Class Constructor

Aug 28, 2014

can we pass private final class object to another class constructor?

View Replies View Related

Create A Code With Case Commands / Public Is Illegal Start Of Statement

Jul 17, 2014

I'm trying to create a code with case commands but it says that public is an illegal start of statement, What do i do?

View Replies View Related

One Public Class Per File

Feb 8, 2014

The compiler won't let me declare more than one class as "public". Am i correct in understanding that this is a java restriction ? This means i need to create a new file, for each public class that i want in a package ? The rest of the classes without access modifier will all be package-private. (Q has been asked before probably, but my search could not be narrowed).

View Replies View Related

Why Cannot Access A Public Method From Another Class

Feb 12, 2015

Why can't I access a method from another class? For example, I want to get the value of get method from another class. It's giving me an error on if(getExamType() == 'M') That's what I've done, for example:

Java Code:

public static Exam[] collateExams(Exam[] exams){
Exam [] r = new Exam[50];
r = exams;
Exam [] finalExam = new Exam[50];
for(int i = 0; i < r.length; i++) {
if(getExamType() == 'M') {
}
}
return r; mh_sh_highlight_all('java');

View Replies View Related

Non-Public Class In A Source File Doesn't Matching Its Name

Oct 20, 2014

I have a question about the following snippet concerning the steps the javac compiler follows to compile a program:

[...]at first, searching a class within a package is discussed if the latter doesn't contain a full package name[...]

It is a compile-time error if more than one class is found. (Classes must be unique, so the order of the import statements doesn't matter.)

The compiler goes one step further. It looks at the source files to see if the source is newer than the class file. If so, the source file is recompiled automatically. Recall that you can import only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compiler can easily locate source files for public classes.

However, you can import nonpublic classes from the current package. These classes may be defined in source files with different names. If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class. I don't quite understand the red fragment. I wondered if the word "import" nonpublic classes from the current package weren't a synonym for the word "use", since why would we want to import a class from the same package when compiler searches the current package automatically anyway?

However I wanted to test nonpublic classes that are contained in source file which name doesn't match the class name:

NonpublicClass.java:

Java Code:

package com.work.company;
class NonpublicClass
{
public void description() {
System.out.println("Working!");
}
} mh_sh_highlight_all('java');

[Code] ....

Everything's fine when the source file names are the same as above. However, when I change NonpublicClass.java to a different name, there's an error "cannot find symbol" in:

Java Code: NonpublicClass v = new NonpublicClass(); mh_sh_highlight_all('java');

I noticed that the class file for NonpublicClass isn't even generated so that's probably the cause. If I change to the directory of the package the NonpublicClass is contained in and compile it directly, i.e. issue for example:

Java Code: javac NonpublicClass_different_name.java mh_sh_highlight_all('java');

Then a proper class file is generated with the class name, that is NonpublicClass.class, and afterwards I can compile the main program in Start.java.

So the question is what am I missing regarding the cited quote that reads:

If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class.

? Because adding such an import to CompanyClass.java:

import com.work.company.NonpublicClass;

didn't work either...

View Replies View Related







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