Computer Engineering 2005 Forum Index Computer Engineering 2005
Bilkent University
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

scale program

 
Post new topic   Reply to topic    Computer Engineering 2005 Forum Index -> Help needed....
View previous topic :: View next topic  
Author Message
salih_safa_bacanli



Joined: 17 Sep 2005
Posts: 20
Location: Computer Intelligence Agency

PostPosted: Sun Apr 30, 2006 9:15 pm    Post subject: scale program Reply with quote

I have tried to make a program for my father that calculates the datas of a scale given by the user. But whatever the datas will be the output is the same which is 0. I could no understand where the error is....
I would be grateful if you can help me
Code:

import java.util.Scanner;
import java.io.*;


/**
* ...Okumadescription...
*
* @author Salih Safa BACANLI...
* cifte karsılastırma deger olcegi
* @version 1.00, 2006/04/29
*/
public class Okuma
{
   
   public static void main( String[] args)throws IOException
   {
      
      System.out.println("********************************");
      System.out.println("********************************");
      System.out.println("cifte karsilastirma deger olcegi");
      System.out.println("olcek hesaplama programi");
      System.out.println("Hazırlayan Salih Safa BACANLI");
      System.out.println("her hakkı mahfuzdur.");
      System.out.println("Versiyon 1.0");
      System.out.println("********************************");
      System.out.println("********************************");
      System.out.println("");
      System.out.println("");
      int uyum = 0;
      int guc = 0;
      int basari = 0;
      int heyecan= 0;
      int gelenek= 0;
      int evrensel= 0;
      int haz= 0;
      int guven= 0;
      int hayir= 0;
      int ozerk= 0;
      
      int uc = 3;
      int dort = 4;
      
      hesapla(uc,dort);
      System.out.println(uc + "    " +dort);
      
      hesapla(uyum,guc);
      hesapla(guc,basari);
      hesapla(basari,ozerk);
      hesapla(ozerk,heyecan);
      hesapla(heyecan,haz);
      hesapla(haz,guc);
      hesapla(guven,gelenek);
      hesapla(gelenek,evrensel);
      hesapla(evrensel,hayir);
      hesapla(hayir,uyum);
      hesapla(uyum,basari);
      hesapla(guc,ozerk);
      hesapla(basari,heyecan);
      hesapla(ozerk,haz);
      hesapla(heyecan,guven);
      hesapla(haz,gelenek);
      hesapla(guven,evrensel);
      hesapla(gelenek,hayir);
      hesapla(evrensel,uyum);
      hesapla(guc,heyecan);
      hesapla(uyum,ozerk);
      hesapla(basari,haz);
      hesapla(ozerk,guven);
      hesapla(heyecan,gelenek);
      hesapla(haz,evrensel);
      hesapla(guven,hayir);
      hesapla(gelenek,uyum);
      hesapla(basari,guven);
      hesapla(guc,hayir);
      hesapla(uyum,heyecan);
      hesapla(ozerk,gelenek);
      hesapla(heyecan,evrensel);
      hesapla(haz,hayir);
      hesapla(guven,uyum);
      hesapla(basari,gelenek);
      hesapla(guc,guven);
      hesapla(ozerk,evrensel);
      hesapla(heyecan,hayir);
      hesapla(guc,gelenek);
      hesapla(uyum,haz);
      hesapla(basari,evrensel);
      hesapla(guc,haz);
      hesapla(ozerk,hayir);
      hesapla(hayir,basari);
      hesapla(evrensel,guc);

      System.out.println("guc: " + guc);
      System.out.println("uyum: " + uyum);
      System.out.println("evrensellik: " + evrensel);
      System.out.println("geleneksellik: " + gelenek);
      System.out.println("basari: " + basari);
      System.out.println("haz: " + haz);
      System.out.println("heyecan: " + heyecan);
      System.out.println("ozerklik: " + ozerk);
      System.out.println("hayirseverlik: " + hayir);
      System.out.println("guvenlik: " + guven);
      
      System.out.println( "Okuma" );       
   }


   public static void hesapla(int bir, int iki)
   {
      
      String biriisim;
      String ikiisim;
      biriisim = bir + "";
      ikiisim = iki + "";
      
      Scanner scan = new Scanner(System.in);
      System.out.print( " ilkinden alinan puan :" );
      int deger;
      deger = scan.nextInt();
      bir = deger + bir;
      iki = iki + (8 - deger);
   }
} // end of class Okuma
Back to top
View user's profile Send private message
murat_ak



Joined: 01 Feb 2006
Posts: 6

PostPosted: Sun Apr 30, 2006 11:42 pm    Post subject: Reply with quote

In Java, primitives are passed by value(only their values are passed, so your changes in the method does not affect the actual parameters- the ones you write inside the call). Since you are trying to change your int variables, you simply cannot do it by passing themselves as parameters.

You can put them in a global array and pass their index as parameters, or you can put them in an object and pass the objects(objects are passed by reference -at least you can suppose so- I think you will fully understand this when you learn pointers in c/c++ in second year. For now, just suppose), etc.

For example, the following changes would work. (first idea above: this seems to require less changes than the second approach.)

Code:

public class Okuma
{
   static final int uyum = 0; // *
   static final int guc = 1;
        ...
   static final int ozerk= 9;

        static int table[];
        public static void main...

              table = new int[10];
              ...     
              System.out.println("haz: " + table[haz]);
              System.out.println("heyecan: " + table[heyecan]);
              ...
     }

     public static void hesapla(int bir, int iki)
     {     
           String biriisim;
           String ikiisim;
           biriisim = table[bir] + "";
           ikiisim = table[iki] + "";
     
           Scanner scan = new Scanner(System.in);
           System.out.print( " ilkinden alinan puan :" );
           int deger;
           deger = scan.nextInt();
           table[bir] = deger + table[bir];
           table[iki] = table[iki] + (8 - deger);
           //System.out.println("bir:"+ bir + "    iki:"+ iki);
      }
}


* remember it is a useful convention to use capital letters for constant/final values in java. so it is better to change uyum->UYUM, guc -> GUC, etc.
Back to top
View user's profile Send private message
mert_ozkaya



Joined: 17 Sep 2005
Posts: 208

PostPosted: Mon May 01, 2006 5:46 am    Post subject: Reply with quote

murat_ak wrote:
In Java, primitives are passed by value(only their values are passed, so your changes in the method does not affect the actual parameters- the ones you write inside the call). Since you are trying to change your int variables, you simply cannot do it by passing themselves as parameters.

You can put them in a global array and pass their index as parameters, or you can put them in an object and pass the objects(objects are passed by reference -at least you can suppose so- I think you will fully understand this when you learn pointers in c/c++ in second year. For now, just suppose), etc.



Java passes all VARIABLES (local or fields) by value. Variables in Java contain either primitive values or references to objects or arrays. Therefore, pass by value in Java causes the value of the primitives or the reference to the object/array to be passed to the function

Here's a classic example of why that's not true(Pass by references ):

Code:

static void swap(Object x, Object y) {
    Object t = x;
    x = y;
    y = t;
}
 
public static main(String[] args) {
    String a = "A";
    String b = "B";
    swap(a, b);
    System.out.println("A = " + a);
    System.out.println("B = " + b);
}




Java uses pass-by-value by definition. The value of the variable is passed, not a reference to the variable. Whether or not at some point in the implementation a value is copied has nothing to do with it. Java passes primitives and references by value and does not pass objects at all.


Mert,

Regards
Back to top
View user's profile Send private message
mert_ozkaya



Joined: 17 Sep 2005
Posts: 208

PostPosted: Mon May 01, 2006 5:54 am    Post subject: Reply with quote

http://javadude.com/articles/passbyvalue.htm
http://java.sun.com/developer/JDCTechTips/2001/tt1009. html#tip1
http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html
http://www.cs.toronto.edu/~dianeh/tutorials/params/
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698
http://radio.javaranch.com/channel/val/2004/05/21/1085 125887000.html


There is exactly one parameter passing mode in Java -- pass by value -- and that helps keep things simple.

-- James Gosling, "The Java Programming Language, Second Edition"
Back to top
View user's profile Send private message
mert_ozkaya



Joined: 17 Sep 2005
Posts: 208

PostPosted: Mon May 01, 2006 6:29 am    Post subject: Reply with quote

This can be more beneficial;

All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. For example:
Code:

 class PassByValue {
    public static void main(String[] args) {
        double one = 1.0;
       
        System.out.println("before: one = " + one);
        halveIt(one);
        System.out.println("after: one = " + one);
    }
   
    public static void halveIt(double arg) {
        arg /= 2.0;     // divide arg by two
        System.out.println("halved: arg = " + arg);
    }
}


The following output illustrates that the value of arg inside halveIt is divided by two without affecting the value of the variable one in main:
before: one = 1.0
halved: arg = 0.5
after: one = 1.0

You should note that when the parameter is an object reference, the object reference -- not the object itself -- is what is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show the distinction:
Code:

 class PassRef {
    public static void main(String[] args) {
        Body sirius = new Body("Sirius", null);
       
        System.out.println("before: " + sirius);
        commonName(sirius);
        System.out.println("after:  " + sirius);
    }
   
    public static void commonName(Body bodyRef) {
        bodyRef.name = "Dog Star";
        bodyRef = null;
    }
}


This program produces the following output:
before: 0 (Sirius)
after: 0 (Dog Star)

Notice that the contents of the object have been modified with a name change, while the variable sirius still refers to the Body object even though the method commonName changed the value of its bodyRef parameter variable to null.


Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. If the Java programming language actually had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.


What gosling said was there was one pass by value mode.
(Gosling is the father of the Java)[/i]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Computer Engineering 2005 Forum Index -> Help needed.... All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group