Gaming World Forums

General Category => Technology and Programming => Topic started by: Holy Cow on October 29, 2011, 12:35:18 pm

Title: Some fast Java questions
Post by: Holy Cow on October 29, 2011, 12:35:18 pm
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!
Title: Some fast Java questions
Post by: Biggles on October 29, 2011, 10:36:15 pm
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.)
Title: Some fast Java questions
Post by: Holy Cow on October 30, 2011, 01:24:07 am
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
Title: Some fast Java questions
Post by: Biggles on October 30, 2011, 08:26:26 am
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.
Title: Some fast Java questions
Post by: Holy Cow on October 31, 2011, 07:18:19 pm
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?
Title: Some fast Java questions
Post by: Holy Cow on November 05, 2011, 02:50:31 am
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
Title: Some fast Java questions
Post by: bootstrap bill on November 05, 2011, 03:04:40 am
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.
Title: Some fast Java questions
Post by: Holy Cow on November 05, 2011, 01:57:04 pm
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
Title: Some fast Java questions
Post by: bootstrap bill on November 05, 2011, 09:38:55 pm
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
Title: Some fast Java questions
Post by: Holy Cow on November 06, 2011, 05:38:32 pm
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...
Title: Some fast Java questions
Post by: bootstrap bill on November 06, 2011, 06:25:54 pm
http://download.oracle.com/javase/1.5.0/docs/api/java/util/EventObject.html#getSource()
Title: Some fast Java questions
Post by: bootstrap bill on November 06, 2011, 06:26:29 pm
well that's how I'd do anyway
Title: Some fast Java questions
Post by: Holy Cow on November 06, 2011, 10:26:37 pm
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
Title: Some fast Java questions
Post by: Holy Cow on December 03, 2011, 02:07:20 am

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");
}
Title: Some fast Java questions
Post by: Holy Cow on December 04, 2011, 12:45:03 am
Found the fix. For string comparison it should be type.equals("STOP") not ==.