final values

i need help on an assignment.

basically i just need an example of a basic program that utilizes

the final attribute for a number.

if its necesary i can post my code, but i would rather just see an example and learn to do it myself.

is that confusing?

i dont know much about finals.

thanks

[332 byte] By [cvockrodta] at [2007-11-26 20:18:26]
# 1
Do you understand what 'final' means? http://mindprod.com/jgloss/final.htmlDo you understand how to use it? Show us your work so far and post a specific question.
kablaira at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...
# 2

basically what i know about finals is that they are constants,

and if you change them it changes the whole behavior of the program

kindof like storing a variable on your calculator?

thats what i think i know, if im wrong let me know

anyway.... my work so far hasn't implemented any final values, since i dont know how.

import chn.util.*;

import apcslib.*;

class Work{

private double myHours;

private double myWage;

Work(){

myWage = 1;

myHours = 1;

}

Work(double hours, double wage){

myHours = hours;

myWage = wage;

}

public double calcGrossPay(){

return Math.round(myWage*myHours);

}

public double calcFedTax(){

return Math.round(.04*calcGrossPay());

}

public double calcStateTax(){

return Math.round(.15*calcGrossPay());

}

public double calcNetPay(){

return Math.round(calcGrossPay() - calcStateTax() - calcFedTax());

}

}

import chn.util.*;

import apcslib.*;

public class testWork {

public static void main(String[ ]args){

double wage, hours, fedTax, stateTax;

int choice = 2;

ConsoleIO keyboard = new ConsoleIO();

do{

System.out.println("Want to know how much you made this month?");

System.out.println("What's your hourly wage?");

wage = keyboard.readDouble();

System.out.println("How many hours did you work?");

hours = keyboard.readDouble();

Work jobA = new Work(wage, hours);

System.out.println("Gross Pay =" + "$" + jobA.calcGrossPay());

System.out.println("Federal Tax = " + "$" + jobA.calcFedTax());

System.out.println("State Tax =" + "$" + jobA.calcStateTax());

System.out.println("Net Pay =" + "$" + jobA.calcNetPay());

System.out.println("Have another job? Type 1 for Yes2 for No");

choice = keyboard.readInt();

}while (choice ==1);

System.out.println("See ya! Hope you get a raise!");

}

}

all of this compiles and runs correctly

cvockrodta at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...
# 3

croc,

The way I read it that's not a bad solution... but I can't really say, not knowing the requirements.

0.04 and 0.15 should be constants... if you need the syntax google "java final"... this one http://www.codeguru.com/java/tij/tij0071.shtml looks pretty good to me (at a glance).

keith.

corlettka at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...
# 4
thanks keith, those values are actually the ones i want to assign as finalas they are the percentages of the tax rates.but im just not sure how..ill look at the site.
cvockrodta at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...
# 5

> basically what i know about finals is that they are

> constants,

It depends on context. The final keyword is used in multiple places. When used in a class declaration it means that class cannot be subclassed. When used in a method declaration it means that method cannot be overridden by a subclass. However, when used with a variable it means that variable's value cannot change. This last usage is probably what your assignment is referring to.

It's important to note that while the value of the variable cannot change, if that variable is a reference it "points" to an object. The value of the variable is not the object itself, it's the reference to the object. When such a variable is final you cannot change it's value, the reference, but you certainly can modify the object it points to.

> and if you change them it changes the whole behavior

> of the program

That's not making much sense, are you thinking of a static variable?

> kindof like storing a variable on your calculator?

Huh?

> thats what i think i know, if im wrong let me know

>

> anyway.... my work so far hasn't implemented any

> final values, since i dont know how.

It's very simple, you just mark the variable as "final" using the final keyword:

public class FinalExample {

// A field aka instance variable that is marked final

final int myInt = 0;

// A parameter marked final

public void myMethod(final double myDouble) {

// A local variable that is marked final

final Object o = new Object();

}

}

The most important difference between final and non-final in this context is that a non-final variable's value can be changed and a final variable's value can't be changed. For example, I can't ever change the value of myInt. I can't change the value of myDouble either, it's always going to be the value that was passed to the method.

kablaira at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...
# 6

> thanks keith, those values are actually the ones i

> want to assign as final

> as they are the percentages of the tax rates.

> but im just not sure how..

> ill look at the site.

See above example. You first need to make them a variable though. If you want them to literally be a "constant" then make them static as well.

private static final double STATE_TAX = 0.15;

kablaira at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...
# 7
i got it to work. thankssorry that was confusing for you.
cvockrodta at 2007-7-10 0:42:04 > top of Java-index,Java Essentials,New To Java...