Create A Class That Includes Three Pieces Of Information As Instance Variables

Oct 9, 2014

Creating a class. Specification are given as:

Create a class called Employee that includes three pieces of information as instance variables:

-Employee ID (string type)
-first name (string type) (default value 'John')
-last name (string type) (default value 'Smith') and
-monthly salary (type double).
-No argument constructor that initializes the three instance variables. The employee id should be generated using the following process:

The employee id should be a combination of first initial, last initial and a number starting from 10001 for the first employee and increasing by one for each employee. e.g. if John Smith is the first employee then its id will be JS10001 and if George Brown is the second employee then its id will be GB10002

-Provide get and set methods for each instance variable. The set method for monthly salary should ensure that its value remains positive - if an attempt is made to assign a negative value, leave the original value.

View Replies


ADVERTISEMENT

Accessing Variables From Array Instance Of Class?

Jan 26, 2015

I am having some problem accessing variables from an array instance of a class. Heres what i have done;

In the main class:

Example obj[]= new Example[4];

In the main class constructor:

obj[0] = new Example(0);
obj[1] = new Example(1);
obj[2] = new Example(2);
obj[3] = new Example(3);

In the main update() method:

if(condition)
//update

In the Example class constructor:

private boolean change = false;

In the Example class update() method:

if(x >20)
change= true;

Now, i want to access the variable change from the main class, how do i do it? The 'condition' in the if statement is the condition of wether the change variable ia true or false. How do i access it?

View Replies View Related

Creating Instance Variables And Constructors For Map Class

Mar 27, 2014

I have to create an application that deals with maps.

I first have to create the instance variables for the class.

So very simply if my hashmap is going to consist of football clubs and players. Football clubs being a string value for the key and players being a set of strings for the values. How would I go about creating the instance variable in a class for this?

I can't seem to find anything that specifically deals with instance variables and constructors for maps.

View Replies View Related

Instance Variables Can Be Declared In Class Level Before Or After Use

Jun 3, 2014

From the tutorial:instance variables can be declared in class level before or after use.But the code below does not compile. Why?

Java Code:

public class MainApp {
i=5;
public static void main(String[] args) {
System.out.println("Hello World");
}
int i;
} mh_sh_highlight_all('java');

View Replies View Related

Servlets :: Thread Safety Should Not Use Any Variables Or Objects At Instance / Class Level

Jun 3, 2014

As web server has multiple threads to serve client requests in Thread Pool & to ensure Thread Safety we should not use any variables or Objects at Instance/Class level.But in case of Session Variable which one is the Best Practice as the Session object is used by all the requests to have the same Session ID.

My Code :

public class MyServlet extends HttpServlet {
private static Logger log = Logger.getLogger(ClientRegistrationServlet.class);
private HttpSession session; /* This is used at Instance Level*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

[code]....

View Replies View Related

How To Create A Class To Have At Most One Instance

Sep 25, 2013

Can I declare a class as

public static final?

Because I can declare a variable as public static final pi=3.14;

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 A Code That Includes Checkboxes / Radio Buttons And Drop Down Menus

Dec 15, 2014

i am very new to Java and i need to create a code that includes check-boxes, radio-buttons, and drop down menus. I decided to make a server order form and listed choices of appetizers, entrees, and desserts. I also need to create a button that combines all selected menu items and outputs the final food order. I also need to create a reset button.

View Replies View Related

Create Instance Of Class Used To Populate DefaultListModel When JList Item Selected

Jan 28, 2015

New to Java Swing. What I am trying to achieve here is to create an instance of the class used to populate the DefaultListModel when a JList item is selected. All examples I have seen show how to return the text displayed in the JList which really is of no practical use.

I have achieved what I want to achieve in that I am successfuly creating the selected object within my ListCellRenderer class.

For the purpose of testing I am using a call to the JOptionPane.showMessageDialog method to display the id of the selected object.

So, I launch the JFrame, the JList is populated, I select an item from the JList and the id value is displayed in the prompt. All this works fine except for one thing.

When I click OK on the JOptionPane.showMessageDialog prompt, the prompt disappears and reappears. The system seems to be in a loop. It is behaving as if the change or click event handler on the JList keeps firing.

This happens even without the creation of the object:

BookEntry be = (BookEntry)list.getModel().getElementAt(list.getSelectedIndex());

I have scoured the code and object properties to see if I can figure out where this behaviour is coming from without success.

All there is in the JFrame design view is a single JList with a variable name of "list". Everything else is in code as below:

Here is the .java code. Its not that complex

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.DefaultListModel;
 public class NewJFrame3 extends javax.swing.JFrame {
 
[Code] .....

View Replies View Related

Student Class That Includes Boolean

Feb 7, 2015

Create an abstract class called Student. The Student class includes a name and a Boolean value representing full-time status. Include an abstract method to determine the tuition, with full-time students paying a flat fee of $2,000 and part-time students paying $200 per credit hour. Create two subclasses called FullTime and PartTime. Create an application that demonstrates how to create objects of both subclasses."

public abstract class Student {
private String name;
private int credits;
public Student(String name){
this.name = name;
credits =0;

[code]....

View Replies View Related

New Object With Instance Variables

Apr 7, 2014

So far in my assignment I have successfully opened a text file. However I am required to do more:

1) As each line of text (containing names and ages) is read a new Runner object is created with its instance variables set thus: ! (Runner class already created )!

- name : set directly set from the value in the file
- agaGroup : can be worked out from the given ages:
< 18 should be 'junior'
> 55 should be 'senior'
the rest should be 'standard'

2) the instance of Runner should be added to the list referenced by the instance variable runners.

I have used if statements to create the junior list, however I do not see the full list of names and ages in the variable runners as I am requested to.

I am sure there is a for loop involved somewhere but I do not know how to:

a) use the for loop in my method
add a new Runner object with the variable mentioned.

I include the code I have done so far as a file - p.s I use Bluej.

public class MarathonAdmin
{
// instance variables
private String runners;
private String age;

[Code] ....

View Replies View Related

Accessing Instance Variables And Methods

Oct 24, 2014

I have the following 2 classes:

class Address {
private int a;
public void set_a(int a) {
this.a = a;
}
}
class Person {
private Address address;
}

How do i access the method set_a() (through the "address" in Person) from another class which contains main() ?

View Replies View Related

Describing The Type Of Instance Variables?

Oct 9, 2014

What is the correct way of describing the type of instance variables?

It is obvious in the case of a primitive type

Example: private int number;

The (data)type of number is integer.

What if the instance variable is an object or a wrapper?

example: private Person person;

the (data?)type of person is an object of the type/class Person?

example: private Integer number;

View Replies View Related

Do Inherited Methods Use Their Instance Variables?

Jan 8, 2014

Do inherited methods use their instance variables or do they use the ones in the method that inherits them?

For example, Class B extends Class A. Class A and B both have the instance variable "potato". A client program tries to use method "cut" using an object of Class B, but class B has no cut method. So, class B uses the "cut" method inherited from class A. What I want to know is will that cut class A's potato or class B's?

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

Why Overridden Doesn't Apply To Instance Variables

Mar 15, 2014

why overridden doesn't apply to variables. However, instance variables are stored inside the object.I ran below program and expected to print "two" but it gets printed "one".

class SupCont {
String s = "one";
}
class Cont extends SupCont {
public static void main(String a[]) {
String s = "two";
SupCont c = new Cont();
System.out.println(c.s);
}
}

View Replies View Related

Making A Rectangle - Manipulation Of Instance Variables

Sep 11, 2014

I am suppose to create a rectangle and I have created two classes; Rectangle.java and RectangleTester.Java.

So far my code for the class Rentangle.java is:

package edu.sbcc.hw2;
public class Rectangle {
private int width = 25;
private int height = 25;
public rectangle(int xcoord, int ycoord, int thewidth, int theheight) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;

So for my assignment I need two instance variables for height and width for which I have, but it says in the assignment I need methods (settings and getters /mutators and accessors that allow manipulation of my instance variables which is a little confusing. Do I put these methods on Rectangle.java or RectangleTester.java.

The same goes for the calculateArea, where am I suppose to put this?

View Replies View Related

Instance Variables Passed In As Arguments To Static Methods?

Feb 19, 2011

I thought static methods could never use instance variables, because they wouldn't know which instance to look at.

From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."

Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?

View Replies View Related

Create Only One Instance

Sep 18, 2014

.I was reading head first java book and saw a barbell question on page no. 280,question-"what if you want to write a class in such a way that only one instance of it can be created,and anyone who wants to use an instance of the class will always use that one,single instance?"

View Replies View Related

Servlets :: How To Create A New Instance For Each New Request

Sep 26, 2014

I want to create a new instance of a Java model class for each new request coming to a servlet.

How to do that without doing that in doGet() or doPost().

View Replies View Related

StringHandler - Create Instance Of Object In A Loop

Oct 26, 2014

I can't figure out where to create the StringHandler object. My code should take a string as input, then create StringHandler object ord with the string input. This should repeat until cancel is pressed, then ord should be sent to the Utskrift-method (a print method).

If I do like this, null is also sent to Utskrift. I dont want that to happen.
If I put StringHandler last in the loop ord can not be resolved.

String text = "";
while (text != null){
text = showInputDialog(null, "Enter text:");
StringHandler ord = new StringHandler(text);
if (text == null){
[Utskrift(ord.getNumber(), ord.getString(), ord.getWords());
break;
}
}

View Replies View Related

How To Pass Object Type To A Method / Then Create Instance

Aug 9, 2014

Essentially, the code I want is:

public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}

ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.

View Replies View Related

Create A New Instance Carte Of Object Carti Using Constructor

Mar 30, 2014

what have I done wrong n the following code? I'm trying to create a new instance carte of object Carti using the constructor and then to insert a row into a table created with SQL.The error I'm getting is:

Exception in thread "main" java.lang.NullPointerException
at Carti.Carti.InsertCarti(Carti.java:103)
at Main.main(Main.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 28 seconds)

The line Main.main(Main.java:37) is when I try to insert the row.
The line Carti.Carti.InsertCarti(Carti.java:103) is when I do the
PreparedStatement st = conn.prepareStatement("insert into Carti (Id,titlu"
+ ", descriere, autor, editie, anPublicare) values (?,?,?,?,?,?)");

Here is the code (Main and Carti Classes)

import Carti.Carti;
import Imprumut_Carti.Imprumut_Carti;
import Membrii.Membrii;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.sql.SQLException;
import java.text.DateFormat;
 
[code]....

View Replies View Related

Create A Program That Keeps Track Of Information Input By User

Sep 17, 2014

I am new to Java an have to Create a program that keeps track of the following information input by the user: First Name, Last Name, Phone Number, and Age. Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns.You should be able to add and remove contacts in the array.

View Replies View Related

Create Instance Of Array Of Several Integers And Prints Data Based On Methods

Apr 15, 2014

I have to make two classes. The first one crates an instance of an array of several integers and prints data (average, greatest, lowest, et cetera) based on the second class, which contains the methods. I'm having a problem with the syntax for the first class required to use the methods.

Here's a shortened version of what I have right now just based on processing the number of integers in the array (because if I can get just one method properly connected, I could figure out everything else).

Driver

import java.util.Arrays;
public class ArrayMethodsDriver
{
//Creates the ArrayMethods object
public static void main(String[] args)
{
int[] a = {7,8,8,3,4,9,8,7};

[Code] ....

When I try to compile this, I currently get the "class expected" error on the count part.

View Replies View Related

Inheritance In Java - Child Class Get Copy Of Methods And Variables Of Parent Class?

Mar 1, 2015

Does child class gets a copy of the methods and variables of parent class?

public class test1 {
public static void main(String a[]) {
Child c = new Child();
c.print();

[Code] ....

why is the output 1?

View Replies View Related







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