i'm learning java and i have a question

Discussion in 'OT Technology' started by Deepsix, Feb 6, 2007.

  1. Deepsix

    Deepsix OT Supporter

    Joined:
    Jan 22, 2004
    Messages:
    19,537
    Likes Received:
    0
    Location:
    Canada
    i'm learning java and i has a question

    how can i write this more efficiently
    Code:
    public class Equations{
     public static void main (String args []){
      int i = 10;
      int j = 6;
      double x = 34.33;
      double y = 10.11;
      double answer1,answer2,answer3,answer4,answer5,answer6;
      answer1 = ((double)i / j) * x;
      answer2 = (x - y);
      answer3 = (x / y / j);
      answer4 = (i + j + x + y);
      answer5 = ((x - y) * j);
      answer6 = (x - (x + y));
      System.out.println (answer1);
      System.out.println (answer2);
      System.out.println (answer3);
      System.out.println (answer4);
      System.out.println (answer5);
      System.out.println (answer6);
     }
    }
     
    Last edited: Feb 8, 2007
  2. teamelement3

    teamelement3 BFB

    Joined:
    Feb 27, 2005
    Messages:
    18,079
    Likes Received:
    0
    don't bother storing answer1-answer6 in variables if you're only just going to print them right after, and not use them anywhere else.
     
  3. Deepsix

    Deepsix OT Supporter

    Joined:
    Jan 22, 2004
    Messages:
    19,537
    Likes Received:
    0
    Location:
    Canada
    can you give me an example of what that would look like?
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Um...replace (answer1) with (((double)i / j) * x), and so on.
     
  5. Deepsix

    Deepsix OT Supporter

    Joined:
    Jan 22, 2004
    Messages:
    19,537
    Likes Received:
    0
    Location:
    Canada
    i just started learning :o

    i tried to do that before but i never put double, thanks
     
  6. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    Ahh...the good ole days.
     
  7. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    As a general rule, with any C-based language, you can declare variables inline.
     
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Woe is me when I see someone learn Java as a first language.
     
  9. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I can only imagine your pain when you see someone learn VB as a first language, then. :rolleyes:

    Y'know, Peyomp, a lot of people who learn a programming language like Java or VB never progress beyond it, but not because their understanding of computer logic is damaged, but because they're simply not interested in doing it professionally. So, is it better for people like this to be confused by more rigid languages and be unable to write a program even to rename their mp3s automatically, or is it better to cut them some slack and let them have a flexible, friendly language that they can actually do some basic (yet useful) tasks with?
     
  10. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Actually, I wouldn't be happier seeing them learn C. But Java is a grotesque language, and its inefficient to accomplish these kinds of tasks in it.
     
  11. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Which language would you recommend for someone who knows nothing of programming, and who may or may not eventually write complex programs?
     
  12. zero xeal

    zero xeal Guest

    fuck optimizations your learning java, get yourself a noose.
     
  13. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Actually, visual basic would be preferable.
     
  14. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Really? Why? .NET or 6.0?
     
  15. Deepsix

    Deepsix OT Supporter

    Joined:
    Jan 22, 2004
    Messages:
    19,537
    Likes Received:
    0
    Location:
    Canada
    i want to write a program where i input a sentence and then it returns with the most frequent uppercase letter in the sentence

    i don't know where to start :o

    i have this
    Code:
    import javax.swing.*;
    
    public class Ucase{
      public static void main (String args[]){
        String s;
        String sentence;
        int count;
        int upperCaseCount = 0;
        
        s = JOptionPane.showInputDialog ("Give me a sentence");
        
        for (int i=0; i<s.length(); i++){
                 if (s.charAt(i) == 'D'){
                   upperCaseCount++;
                 }
           }
        
        System.out.println ("Input: " + s);
        System.out.println ("Number: " + upperCaseCount);
      }
    }
    i can get it to count the D's but what would it look liek so it would count every letter and determine the most frequent
     
    Last edited: Feb 8, 2007
  16. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Neither. Regular BASIC like an Apple ][ has. Why? Because abstracting everything as an object runs counter to teaching people to implement logic, is horribly inefficient and nonsensicle to a beginner, and all the GUI shit is just extra crap you don't need when you are learning.
     
  17. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    logowriter
     
  18. Deepsix

    Deepsix OT Supporter

    Joined:
    Jan 22, 2004
    Messages:
    19,537
    Likes Received:
    0
    Location:
    Canada
    i learned that in highschool a couple years ago
     
  19. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    :rofl:
    gosub FTW!

    Abstraction is abstraction whether it's done with functions and procedures or classes. Noone begins by teaching students classes, they say, "Look this is how you get your program running, it's a class but don't worry about that now, we'll talk about them in a couple of weeks. Till then, just do this to get your program working. For now, focus on the logic within the class."

    Students will learn logic by solving programming problems. Java does not remove the burden of logical programming.
     
  20. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    To the OP, in your original program: Don't define 6 different answer variables. Simply define one, then keep changing it's value after you display it. So it would look like this:
    Code:
    double answer = null; //it's good programming practice to always initialize 
                         //your variables, eventhough Java does this for you.
    answer = ((double)i / j) * x;
    System.out.println (answer);
    
    answer = (x - y);
    System.out.println (answer);
    //etc.
    
    You can also combine your calculation in your println instructions which removes the need to store the answer at all.

    I'm reasonably certain this will work...too lazy to code it up to be sure.
    Code:
    System.out.println ("(x+3/2)*4");
    
    The only reason to define 6 different answer variables is if you will need those numbers later. If you are simply displaying them, you don't need them after they are displayed.
     
  21. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    :werd:

    Visual Basic actually solves a lot of the "spaghetti code" problems that old-style BASIC has; I find myself stumbling over BASIC PDS' inadequacies when I have rare occasion to be writing a command-line program (since Windows doesn't really have shell-scripts, per se).

    Any language is going to require a solid understanding of logic, because classes and structs and pointers and other shit like that are just ways to re-use logical algorithms once they've been written. They don't replace logical algorithms; in fact, they probably enhance the user's ability to write proper code that doesn't use "magic numbers", because in order to use a single algorithm in many contexts, the user has to use variables instead of hard-coded values.

    - - -

    This actually ties in well with the OP's question about how to search for the most common uppercase letter, instead of just searching for 'D':

    1. Build a "struct" that holds two variables: a "char" and an "int".
    2. Declare a variable consisting of a 26-element array of this struct.
    3. Run a "for" loop with an index of 0 to 25, that will assign 'A' + [index] to the "char" variable inside each of the elements in the array.
    4. Run another loop, also with an index of 0 to 25, that will search the sentence for whatever letter is assigned to the "char" variable in each element of the array, and then store the number of occurrences in the "int" variable.
    5. Run a third loop that scans the array, looks for the biggest "int" variable, and displays the "char" variable stored in the same array element as the largest "int".
     
    Last edited: Feb 8, 2007
  22. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    First you need an ASCII character key, and to find out what the Java ASCII command is, usually it's ASC. With the ASCII keys you want to find the range for all uppercase'd letters, they should be stringed together.

    You'd want a second for .. loop to start at the first capital letter 'A' (whatever the ASCII key is for the letter 'A') and end at the last capital letter, 'Z'. Then you reference the for counter (you'd probably use k) and say something like:

    Code:
                 if (s.charAt(i) == ASC(k)){
                   upperCaseCount++;
                 }
    
    Oh, you'd also want to replace upperCaseCount with an array. So something like upperCaseCount[ASC(k)]++; That'd produce an array with each capital letter and the number of times it occured, you'd then be able to sort on it, print from it, etc.

    I'm not writing it cuz it appears to be school work but this should get you started.
     

Share This Page