Beginner java sequentially incrementing a set variable and resetting the count - java

I am having a few minor issues with my class methods which I am to increment sequentially. Basically my class compiles, there is a test class and when I run the tester to get the test results I do not get the desired output I am looking for and it is followed by an error message which I will provide below.
My question is when I create an object it starts off with the the prefix + counter but does not increment it right away, I want it to be so that it increments the first object created to 1001 right away and so on and so forth. These error messages are confusing me on how the expected value is something totally different from my desired output.
I have the prefix set and a counter which starts at 1000.
public static final String TICKET_PREFIX = "VAN";
public static int counter = 1000;
public ParkingTicket(){
ticketNumber = generateTicketNumber();
}
}
private String generateTicketNumber(){
ticketNumber = TICKET_PREFIX + counter++;
return ticketNumber;
}
Here is the code from the test class code block where the error is caught
#Test
public void testConstructorTicketNumberSEquential() {
ParkingTicket.resetCounter();
ParkingTicket ticket = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
ParkingTicket ticket2 = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
ParkingTicket ticket3 = new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 );
assertEquals("VAN1001",ticket.getTicketNumber());
assertEquals("VAN1003", ticket3.getTicketNumber());
and this is the error message from the tester
testConstructorTicketNumberSEquential
---
org.junit.ComparisonFailure: expected:<VAN100[1]> but was:<VAN100[7]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at ParkingTicketTest.testConstructorTicketNumberSEquential(ParkingTicketTest.java:238)
Also I have a method to reset the counter. I tried just returning the counter value but I am still getting an error
public static int resetCounter(){
int reset = counter;
return reset;
}
test for the reset counter
#Test
public void testResetCounter() {
ParkingTicket.resetCounter();
assertEquals(1000,ParkingTicket.counter);
}
test run error message
testResetCounter
---
java.lang.AssertionError: expected:<1000> but was:<1005>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at org.junit.Assert.assertEquals(Assert.java:542)
at ParkingTicketTest.testResetCounter(ParkingTicketTest.java:264)

You currently never set the counter back to the initial value inside of resetCounter(), and you never use the return value.
In my opinion, the resetCounter() should be a void method, and simply set counter back to an initial value which is contained in a constant.
I named this constant INITIAL_COUNTER:
public static final String TICKET_PREFIX = "VAN";
public static final int INITIAL_COUNT = 1000;
public static int counter = INITIAL_COUNT;
public static void resetCounter() {
counter = INITIAL_COUNT;
}
public static void main(String[] args) {
counter++;
counter++;
System.out.println(counter);
ParkingTicket.resetCounter();
System.out.println(counter);
}
Output:
1002
1000
Here from the example main I made you can see resetCounter properly resets counter back to 1000. Simply change the value of INTIAL_COUNT to change your default count value.
On another note, you wrote you expect VAN1001 to be the ticket value of your first new ParkingTicket("Adam White","VAN5225", "1A2B3C",20 ) when you should actually expect it to be VAN1000.
This is due to using a post-increment on count instead of a pre-increment on the line below:
ticketNumber = TICKET_PREFIX + counter++;
If you want the first ticket to start at 1001 instead, change count++ to ++count, which will increment count by 1 before assigning the ticketNumber value.

Related

code block getting executed twice in a single threaded program

In my below program i observe that the value of score variable is twice(999000 instead of 499500) it's expected value. Closer look at it suggests that the computation is done twice even though the flag gets set to true after the first invocation. Any idea what is going wrong here? The program is single threaded. Actual computation involves invoking a rest API, but for testing purpose i have removed it.
public class DataClient {
public static void main(String[] args) {
System.out.println(CalculationCache.getScore());
}
}
class CalculationCache{
static{
computeScore();
}
private static int score;
public static int getScore() {
computeScore();
return score;
}
private static boolean flag=false;
static void computeScore(){
if(!flag) {
//calculate the score
for (int i = 0; i < 1000; i++) {
score = score + i;
flag = true;
}
}
}
}
The issue is due to the ordering of class initialization. The static initializers are executed in the order they are defined. The variable flag is initialized only after computeScore() is invoked. Hence flag will be false when the method is called the second time. You might want to get rid of the static block
static{
computeScore();
}
if you want lazy initialization.

Java code does not return anything even though it is supposed to

I'm just new to Java OOP, and I hardly understand about the class and stuff. I tried to write some code to understand but I didn't succeed. Here is my code, I expected it to return the number of eggs but I don't know why it returns nothing.
class EggCounter {
private int egg;
{
egg = 0;
}
public void eggAdd()
{
egg = egg + 1;
}
public void eggBreak()
{
egg = egg - 1;
}
public void eggAddDozen()
{
egg = egg + 12;
}
public int getEgg()
{
return egg;
}
}
public class EggTest
{
public static void main(String[]args)
{
EggCounter egg = new EggCounter();
egg.eggAdd();
egg.eggAddDozen();
egg.eggBreak();
egg.getEgg();
}
}
It does return 12. Replace the egg.getEgg(); line in your main method with System.out.println(egg.getEgg()); and you will notice it prints 12.
It is returning, it's just that you do nothing with the return value of getEgg. What you need to do is store it into the variable or do something with it. return <value> only returns the given value to the callee, you must store it to use it. Example:
int eggCount = egg.getEgg();
System.out.println(eggCount);
Here, the assignment of eggCount calls egg.getEgg(). The call resolves when the number of eggs is returned, which assigns the return value to eggCount. Finally, it will print out eggCount. If you need the result of egg.getEgg() later, you can simply just output the following:
System.out.println(egg.getEgg());
How this works is the method egg.getEgg() is called. The return value is then resolved, which is passed into the print statement. This gets rid of storing it into a variable you can use later.

Modify non-static variable from inside static main function

I want to modify a non-static variable from inside my static main function and then return that modified value for use elsewhere. Here is the code:
package servletPackage;
public class Blah {
private int count = 5;
public static void main(String[] args) {
Blah blah = new Blah();
blah.count = 10;
}
// return Count
public int getCount()
{
return count;
}
}
However, when I access this count from a different file, I get 5 when I want to get 10. Its like count will only stay 10 inside the main, then it changes back to 5. How do I change it so that I can return 10 instead? I tried to get rid of the static in main but then it wouldn't run. Any help is appreciated. Thank you!
Edit: here is the javascript code that receives the returned count value of 5 instead of 10
function update3() {
blah.getCount(function(data) {
dwr.util.setValue("demoReply2", data);
});

Java Static object/method issue

I got a question about how to use static, I saw a sample:
public class Exe {
static int i = 47;
public void call() {
for (i = 0; i < 3; i++) {
if (i == 2) {
System.out.println("\n");
}
}
}
public Exe() {
}
public static void main(String[] args) {
Exe t1 = new Exe();
Exe t2 = new Exe();
t2.i = 60;
System.out.println(t1.i);
t1.call();
System.out.println(t2.i);
t2.call();
}
}
When I tried to run it, it printed 60 3, I am wondering why t2.i here is 3, I do not know where the 3 comes from, also, the both results of t1.call() and t2.call() were not printed, please advise, thank you!
for(i=0; i<3;i++){
if(i==2){
System.out.println("\n");
}
}
Your static variable Assigned/incremented here.
Not the i (which you are assuming, it's different) in for loop.
To clear the clouds, Just take another variable called j and do the looping.
for (int j = 0; j < 3; j++) {
if (j == 2) {
System.out.println("\n");
}
}
static means that every instance of the class has access to the same, single instance of the variable.
When you create t1, t1.i is initialized to 47. Then you create t2. t2.i and t1.i are the same variable, so whether you do t1.i = 60 or t2.i = 60, they're BOTH equal to 60.
So before you do t1.call(); or t2.call(), the first thing you do is print out t1.i, which is 60, as per the line t1.i = 60;.
Then you run t1.call() which runs through the for loop. The for loop exits when i can't pass the test i < 3, and since i is an integer, this happens as soon as i is incremented to 3.
After you've run t1.call(), i is now equal to 3. This means both t1.i and t2.i since static means there's only one copy of i across all instances of the Exe class here. So you print out t2.i and it is equal to 3, as it should be.
Hope this helps.
The 3 comes from your for loop, which is reusing the same static int i that you're manually setting to 60 before you call call(). The results of the println probably are being printed, but they're just blank lines.
for (i = 0; i < 3; i++)
In this statement instead of treating i as a local variable it is taken to be class variable which you defined. As for class variables only one copy is maintained for all the instance after the for loop finished(i=3) value of you class variable i remains to be 3.
Your static variable is the same across all objects, this is an effect of static. This means that when you call t2.i = 60; both t1.i and t2.i are set equal to 60. Then when you call t1.call(); you again change both objects. As others have explained your for loop is setting the variable i = 3.
It's because it's a static variable that the assignment affects both objects
Static members doesn't reflect the object state!
All objects of a class will have the same copy(mean to say same value) of the static variables. If one object changes the value of a static variable, since the static variable holds the same value across all the objects, the static variable value will be updated across all the objects of that class. This rule reflects your result.
See the below example:
public class StaticTest {
static int staticA = 10;
int intA;
public StaticTest(int intA){
this.intA = intA;
}
public static void main(String[] args) {
StaticTest test1 = new StaticTest(10);
System.out.println(test1.intA);
System.out.println(test1.staticA);
StaticTest test2 = new StaticTest(20);
System.out.println(test2.intA);
System.out.println(test2.staticA);
}
}
Below is the outpput:
10
10
20
10

Static public and private access in example

The question is:
Write the definition of a class Counter containing:
An instance variable named counter
of type  int .
An instance variable  named  limit of type  int  .
A static  int variable  named  nCounters which is initialized  to 0 .
A constructor  taking two int parameters  that assigns  the first one to counter and
the second one to limit . It also adds one to the static variable  nCounters .
A method  named  increment  . It does not take parameters  or return a value ; if the
instance variable  counter is less than  limit , increment  just adds one to the
instance variable  counter .
A method  named  decrement that also doesn't take parameters  or return a value ; if
counter is greater than  zero, it just subtracts one from the counter .
A method  named  getValue that returns the value  of the instance variable  counter .
A static  method  named  getNCounters that returns the value  of the static variable 
nCounters .
My Dilemma
The code works fine but I want to know the following:
Why is the first static private and the second one public?
My code:
public class Counter
{
private int counter;
private int limit;
private static int nCounters = 0;
public Counter (int x, int y)
{
counter = x;
limit = y;
nCounters++;
}
public void increment ()
{
if( counter < limit)
{
counter++;
}
}
public void decrement ()
{
if(counter > 0)
{
counter--;
}
}
public int getValue ()
{
return counter;
}
public static int getNCounters ()
{
return nCounters;
}
}
nCounters is a variable that you use to count the numbers of times the Class constructor has been called. It's static because is not a instance variable but a class variable (its value is shared by all the instances of that class).
nCounter is privated for encapsulate it. It can only be accessed outside the class by a read only method named getNCounters. Otherwise, I could access the value of nCounter and change it directly causing the value of classes created not reliable anymore.
You can read more about encapsulation and static methods and variables to understanding better your code.
The first static variable nCounters is private, because you want to control it in your class - you do not want any code outside of Counter to modify it. But you want to make it possible to read its value outside of Counter, thus the getNCounters() method is public.

Categories

Resources