QA - Question Some fast Java questions (Read 675 times)

  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
Alright so i'm doing an introductory course on OOP programming and we're obviously doing Java at my uni and i have some fast questions for those that know a little in depth stuffz:


1) We are making a calculator using a premade CalculatorGui, which requires some methods like deleteLastDigit (for the Backspace button). That particular one's body is:


deleteLastDigit(){
buf=new Stringbuffer(buf.substring(0,buf.length()-1)); which actually gives the "buf" reference a new stringbuffer instance which contains the same string without the last character (since it has been deleted by the backspace button). The question is, where does the OLD instance go? Without a reference attached to it i believe it is being annihilated by the garbage collector but i want to make sure hence the question.


2) Inside the CalculatorGui class there is a Class datamember called display which is of TextField type. We actually use this in order to show on the Gui of the calculator the numbers and the results. The function used is setText in order to refresh what is shown on the calculator's display field. The question is wouldn't it be better if this data member was an INSTANCE data member and NOT a CLASS data member? And if yes for what reason? What i wrote was that it would be better for it to be an instance data member since in order to make the calculator GUI appear you have to make a new instance of it, so we already create a new instance to use the setText method on. Also if we want to have more than one instance of the calculator (i.e 2+ calculators at the same time), when using the setText method to refresh one calculator it would refresh all of them since its a class method through the same program. Again want to make sure cause that one was totally out of my head :P.




I know its a bit long but thanks to those who will take some time and help!
  • Avatar of Biggles
  • I know your secrets
  • PipPipPipPipPip
  • Group: Premium Member
  • Joined: May 5, 2005
  • Posts: 688
Java's GC is has changed a lot over time, but as far as I understand it, you can't be sure that the GC will catch the 0 refcount immediately and free the memory. You can probably expect that the GC will free the memory when it thinks it needs to.

Your other one is fine. You should be careful about asking questions like these on the internet so explicitly though. It may be against your university's cheating policy. (It's would have certainly been against mine.)
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
Afaik we are allowed to ask questions on the internet. I did not ask you to write my whole paper i just wanted some stuff answered which has not been touched in class and the teacher wouldn't obviously answer them directly since its in the paper. So what am I supposed to do? The internet serves that exact purpose and I use it for that. Obviously it would not be fair to have you solve an entire problem but asking questions online I believe should be expected of students :S
  • Avatar of Biggles
  • I know your secrets
  • PipPipPipPipPip
  • Group: Premium Member
  • Joined: May 5, 2005
  • Posts: 688
Hey now, I didn't accuse you of cheating. I just knew I was explicitly banned from asking questions like that so I suggested you checked. I didn't want you to get in trouble because of me.
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
Didn't get offended by your comments :P

I've heard stuff like that happening in unis and it amazes me how they want us to find an answer without asking neither the teacher nor online...Do i require divination powers to complete my paper?
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
Alright got another question now on a new project we are working on:

We now transitioned from using a premade GUI to making our own calculator GUI, using the awt. In the example they gave us, the GUI had initialized every single button required by the calculator as a Button instance with a distinct name etc. Along with that he wrote 15 times the same code for designing, positioning, registering, and adding the action listener. We were asked to change that copy-paste stuff with some more programmy stuff (didnt tell us what). So someone mentioned making a FOR loop that will create each instance, position it on the frame of the calculator etc. However I believe java can't create instances by itself using an index integer like buttoni (where i=0,1,2...). So i put them in an array. 2 issues here:

1. I'd like a fast insight on my way of doing this. Is there a better way/more appropriate?

2. In order for each button to work it requires ButtonXHandler classes that implement the Action Listener interface and use ActionEvent method to recognize the click action from the mouse. The question is: Can i avoid writing 10 Button Handler classes like i did with the buttonX instances? I don't think i can create them with a loop but i may be wrong...


Thanks in advance
  • Tobaccotherapy : the cure through smoke
  • Pip
  • Group: Member
  • Joined: Mar 14, 2004
  • Posts: 168
on button click:

add "this" button's "value" to text field

create every button, set the "value" of each one of them to a different digit. That way they all use the same handler function.
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
aspargus i didn't really get your method :S.




for(i = 0;i<10;i++){



button[ i ]=new Button(i.toString());
         this.add(button);  //This registers the button in the calculator's frame
         button.setBounds(new Rectangle(x,y,35,28));
         button.setFont(new Font("calibri", Font.PLAIN, 15));
         button.setBackground(Color.lightGray);
         
         switch(i){
         case 0:
            button.addActionListener(new Button0Handler(this));
            break;
         case 1:
            button.addActionListener(new Button1Handler(this));
            break;
         case 2:
etc


}




and the button handlers:



class Button0Handler implements ActionListener{
   CalculatorGui cg;
   public Button0Handler(CalculatorGui cg){
      this.cg=cg;
   }
   
   public void actionPerformed(ActionEvent pushingButton0){
      CalculatorGui.op.addDigit('0');  //This is a method from another class in order to add the digit in a stack.
     
   }
}




etc.


That's how the code is structured. What do i do exactly? :D
  • Tobaccotherapy : the cure through smoke
  • Pip
  • Group: Member
  • Joined: Mar 14, 2004
  • Posts: 168
I don't really know either but when a button triggers an event, you can access the button object from inside that event handler, so you could make a button click handler that looked like

public void actionPerformed(ActionEvent event){
  CalculatorGui.op.addDigit( event.getSource().dont_know_what_the_label_property_is_called );
}

Also you'll have to cast event.getSource() to a Button.
That way you only write a function for each of the 0...9 buttons
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
It was actually much simpler than that. I just used a single ButtonHandler class and the ActionEvent event was named pushingButtoni. Then i just changed the addDigit method to append an integer in the StringBuffer and all was cool. I hadn't really understood how pushingButton event works and i still don't quite get where it comes from and whatnot. Checked the ActionEvent class from the documentation but it doesn't contain anything like that...
  • Tobaccotherapy : the cure through smoke
  • Pip
  • Group: Member
  • Joined: Mar 14, 2004
  • Posts: 168
http://download.oracle.com/javase/1.5.0/docs/api/java/util/EventObject.html#getSource()
  • Tobaccotherapy : the cure through smoke
  • Pip
  • Group: Member
  • Joined: Mar 14, 2004
  • Posts: 168
well that's how I'd do anyway
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
Maybe but I believe it makes the addDigit method more complicated since it has to analyse what the instance represents. Anyway I saw in the textbook that we will be using the .getSource() method soon in a more advanced (i guess) version of the interface so i'm gonna wait on that for now. Thanks for the help
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54

New project now and got a fast question:


I'm doing some inputs using the BufferedReader type and its readLine() method. I'm inside a nested while loop that goes on as long as a boolean is false. When I input the word STOP in the BufferedReader, right after that I have an


if(type=="STOP") {
flag=true;
break;
}


The break exits the nested loop (which always runs till a break command is encountered) and after that I go into a switch for the "STOP" case that prints a message informing that the program is stopping all input. The problem is that the program seems to skip the IF case all together. After I input STOP, it will continue into the other commands of the loop instead of breaking out of it and then goes to the switch case normally printing the message but will start over the outside loop again which means that the flag hasn't changed from false to true. Code:


while (flag==false){
   while(true){


   try{
      line=in.readLine();
      type=new String(line);


      //I input STOP here


      if (type=="STOP"){
         flag=true;
         break;   //This is where is should break away from the nested loop...
      }
      
      line=in.readLine();
      x=new Integer(line);


      line=in.readLine();
      y=new Integer(line);
      
      
      line=in.readLine();
      inLine1=new Integer(line);
      
      break;
   }
   catch(IOException e){
      e.printStackTrace();
   }
   catch(NumberFormatException e){
      e.printStackTrace();
   }
}


   
   switch(type){
      case "STOP":
         System.out.println("Stopping input");     //This gets printed
         break;
      default:
         System.out.println("No such gate type! Start again");
         break;
   }
   
}  //After printing it will start over instead of exiting the outer while loop...
   System.out.println("Total end");
}
  • Avatar of Holy Cow
  • Let's put a smile on that face...
  • Group: Premium Member
  • Joined: Jun 8, 2008
  • Posts: 54
Found the fix. For string comparison it should be type.equals("STOP") not ==.