1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

critiques please...

Discussion in 'Computers, Games, Electronics etc' started by Dustin_m, Sep 15, 2009.

  1. Dustin_m

    Dustin_m Active Member

    Messages:
    4,603
    Likes Received:
    16
    Joined:
    May 21, 2003
    Location:
    San Diego, CA
    I've started taking both a Java and a C++ class this semester. I'm done with all required classes, so while waiting to get into a school to transfer, I'm taking a couple classes for fun. I thought I'd post up some of the small assignments I've done so far to see if it all looks ok. Yes, this is beginner stuff, but I remember always seeing you guys on here bitching about people developing bad habits from the get go, so I thought I'd put my stuff out here and let you pick it apart. Let me know where I'm doing something wrong or in a way that's inefficient.

    This first one is the only C++ assignment so far, and it's short.
    Code:
    asst1.cpp       -       This program calculates the area and
                            circumference of a circle
    
    Dustin Michelson
    Sep 7, 2009
    ****************************************************************/
    
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        double      radius ;                                //Declares radius variable
    
        cout << "Please enter the radius of a circle\n" ;   //Prompts user for data
        cin  >> radius ;                                    //Gives value to radius
    
        cout << "\nThe area of the circle is:\t\t" << (M_PI * (radius * radius)) << endl ;  //Prints radius to screen
    
        cout << "\nThe circumference of the circle is:\t" << (2 * M_PI * radius) << endl ;  //Prints circumference to screen
    
        return 0 ;
    }
    
    
    and here's the Java ones.

    Code:
    /*coinsToDollars.java 
     *    
     *This program will give the total dollar value of the amount of coins entered by the user
     *
     *Michelson, Dustin
    */
    
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class coinsToDollars
    {
      public static void main( String [] args ) 
      {
          
       Scanner scan = new Scanner( System.in );                        //instantiates a Scanner object named scan   
       DecimalFormat pricePattern = new DecimalFormat( "$#0.00");    //instantiates a DecimalFormat object named pricePattern
       
       
       double quarterValue, dimeValue, nickelValue;        //initializes a variable to hold the total value of each type of coin
       double sum;                        //This will hold the total dollar amount of the coins
       
       System.out.print( "Enter the number of quarters: " );    //Prompts user for number of quarters
       int quarters = scan.nextInt( ) ;                            //Puts user input into quarters variable
       
       System.out.print( "Enter the number of dimes: " );        //Prompts user for number of dimes
       int dimes = scan.nextInt( ) ;                            //Puts user input into dimes variable
       
       System.out.print( "Enter the number of nickels: " );        //Prompts user for number of nickels
       int nickels = scan.nextInt( );                            //Puts user input into nickels variable
       
       quarterValue = quarters * 0.25;        //Changes the number of quarters into a dollar value
       
       dimeValue = dimes * 0.10;            //Changes the number of dimes into a dollar value
       
       nickelValue = nickels * 0.05;        //Changes the number of nickels into a dollar value
           
       sum = quarterValue + dimeValue + nickelValue;    //Calculates the sum of the coins
           
       System.out.println( "The total value of the coins is " 
                                           + pricePattern.format( sum ));        //Prints the total dollar value to the screen
                
               
      }
    }
    Code:
    /**
     * @(#)domainName.java
     *
     *This program will let the user input a name, 
     *then it will turn that name into a domain name by adding
     *a www. to the beginning and a .com to the end
     *
     *
     * @author Dustin Michelson
     * @version 1.00 2009/9/14
     */
    
    
    import java.util.Scanner;
    
    public class domainName
    {
      public static void main( String [] args ) 
      {
        
       Scanner scan = new Scanner( System.in );
        
       System.out.print( "Enter a word that you would like to turn into a domain name: " );
       String domainWord = scan.next( );
       System.out.println( "Your domain name is:  www." + domainWord + ".com" );
      }    
    }
    Code:
    /**
     * @(#)smallerOfTwoRandomNumbers.java
     *
     *This program will randomly generate two numbers between 0 and 100, 
     *and will print the smaller of the two
     *
     *
     * @author Dustin Michelson
     * @version 1.00 2009/9/14
     */
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class smallerOfTwoRandomNumbers
    {
      public static void main( String [] args ) 
      {
        
       Scanner scan = new Scanner( System.in );    //instantiates a new Scanner object
       Random random = new Random( );            //instantiates a new Random object
       
       int number1 = random.nextInt( 100 - 0 + 1) + 0;        //genereates a random number between 0 and 100
       System.out.println( "The first random number is " + number1 );    //Prints the first random number
       
       int number2 = random.nextInt( 100 - 0 + 1) + 0;        //generates another random number
       System.out.println( "The second random number is " + number2);    //prints the second random number
       
       if (number1 < number2)    {            //Determines which word is longer and prints the longer of the two words
                System.out.println ( "The smaller of the two numbers is " + number1 ) ;
                   } else     {
                       System.out.println ( "The smaller of the two numbers is " + number2 ) ;
                           }
       
      }
       
        
        
    }
    Code:
    /**
     * @(#)longerOfTwoWords.java
     *
     *This program will let the user input two words.  It will then determine
     *which of the two words is smaller, and output that word
     *
     *
     * @author Dustin Michelson
     * @version 1.00 2009/9/14
     */
    
    import java.util.Scanner;
    
    public class longerOfTwoWords 
    {
    
        public static void main( String [] args ) 
        {
            
            Scanner scan = new Scanner( System.in) ;
            
            System.out.print( "Enter your first word: " ) ;      //Prompts user for first word
            String firstWord = scan.next( ) ;                    //Puts user input into firstWord
            
            System.out.print( "Enter your second word: " ) ;    //Prompts user for second word
            String secondWord = scan.next( ) ;                    //Puts user input into secondWord
            
            int firstWordLength = firstWord.length( ) ;            //Sends the length of firstWord to firstWordLength as an integer value
            int secondWordLength = secondWord.length( ) ;        //Sends the length of secondWord to secondWordLength as an integer value
            
            if (firstWordLength > secondWordLength)    {            //Determines which word is longer and prints the longer of the two words
                System.out.println ( "The longer of the two words is " + firstWord ) ;
                   } else     {
                       System.out.println ( "The longer of the two words is " + secondWord ) ;
                           }
           }
    }
        
        
    
    Code:
    /**
     * @(#)futureValueOfInvestment.java
     *
     *This program will let the user input an initial investment amount and 
     *an interest rate, and will calculate the value of the investment in 5, 10, and 20 years
     *
     *
     * @author Dustin Michelson
     * @version 1.00 2009/9/14
     */
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class futureValueOfInvestment
    {
      public static void main( String [] args ) 
      {
        
       Scanner scan = new Scanner( System.in );                        //instantiates a new Scanner object
       DecimalFormat pricePattern = new DecimalFormat( "$#0.00");    //instantiates a DecimalFormat object
        
       System.out.print( "Please enter a value for the initial investment in whole dollars: " );
       double initialInvestment = scan.nextDouble( );
       
       System.out.print( "Please enter a value for the interest rate in decimal format: " );
       double interestRate = scan.nextDouble( );
       
       double value5yrs = Math.pow((1 + interestRate), 5) * initialInvestment;
       double value10yrs = Math.pow((1 + interestRate), 10) * initialInvestment;
       double value20yrs = Math.pow((1 + interestRate), 20) * initialInvestment;
       
       System.out.println( "The value of the investment in 5 years will be " 
                                               + pricePattern.format( value5yrs ));
                                               
       System.out.println( "The value of the investment in 10 years will be " 
                                               + pricePattern.format( value10yrs ));
                                               
       System.out.println( "The value of the investment in 20 years will be " 
                                               + pricePattern.format( value20yrs ));
        
        
       }
        
        
    }
    Code:
    /*     This program will calculate and print the area and 
     *    circumference of a circle with radius 3.2 inches
     *
     *    Michelson, Dustin
    */
    public class areaAndCircumferenceOfCircle
    {
      public static void main( String [] args ) 
      {
       final double PI = 3.14159;
       double radius = 3.2;
       double circumference = (2 * PI * radius);     //Equation for the circumference of a circle in terms of the radius
       double area = (PI * (radius * radius));        //Equation for the area of a circle in terms of the radius
       
       System.out.println("For a circle with a radius of 3.2 inches:");
          
       System.out.println("\tThe area of the circle in inches is " + area);                        //Prints the area
          
       System.out.println("\tThe circumference of the circle in inches is " + circumference);    //Prints the circumference
                
               
      }
    }
    Code:
    /* This program will convert 2, 5, and 10 inches to millimeters
     *
     *    Michelson, Dustin
    */
    public class inchesToMillimetersConverter
    {
      public static void main( String [] args ) 
      {
       final double CONVERSION_FACTOR = 25.4;    //There are 25.4mm in 1 inch
       double lengthInMillimeters;
       int lengthInInches = 2;                    //Sets the length in inches to 2 to calculate the length in millimeters
       
       System.out.println("The number of millimeters in 2 inches is " + (lengthInInches * CONVERSION_FACTOR));
          
       lengthInInches = 5;                        //Sets the length in inches to 5 to calculate the length in millimeters
       
       System.out.println("The number of millimeters in 5 inches is " + (lengthInInches * CONVERSION_FACTOR));
          
       lengthInInches = 10;                        //Sets the length in inches to 10 to calculate the length in millimeters
       
       System.out.println("The number of millimeters in 10 inches is " + (lengthInInches * CONVERSION_FACTOR));
                
               
      }
    }
    Code:
    /*     This program will convert 10, 50, and 100 pounds into their equivalent amounts in kilograms
     *
     *    Michelson, Dustin
    */
    public class poundsToKilogramsConverter
    {
      public static void main( String [] args ) 
      {
       final double CONVERSION_FACTOR = 4.54E-1;    //There are 0.454 kilograms in each pound
       double weightInKilograms;
       int weightInPounds = 10;                    //Sets the weight in kg to 10 to convert to pounds
       
       System.out.println("The number of kilograms in 10 pounds is " + (weightInPounds * CONVERSION_FACTOR));
          
       weightInPounds = 50;                        //Sets the weight in kg to 50 to convert to pounds
       
       System.out.println("The number of kilograms in 50 pounds is " + (weightInPounds * CONVERSION_FACTOR));
          
       weightInPounds = 100;                        //Sets the weight in kg to 100 to convert to pounds
       
       System.out.println("The number of kilograms in 100 pounds is " + (weightInPounds * CONVERSION_FACTOR));
                
               
      }
    }
     
  2. jamesA

    jamesA Well known pissed off telephone guy VIP

    Messages:
    16,129
    Likes Received:
    1,020
    Joined:
    Aug 10, 2003
    That is some fascinating Greek.
     
  3. TurboMirage

    TurboMirage YEEAAAHHH VIP

    Messages:
    24,582
    Likes Received:
    697
    Joined:
    May 20, 2003
    Location:
    Central, MA
    oh man, just quit, programmers have no lives
     
  4. Dustin_m

    Dustin_m Active Member

    Messages:
    4,603
    Likes Received:
    16
    Joined:
    May 21, 2003
    Location:
    San Diego, CA
    Yea, it's definitely still Greek to me for the most part.
     
  5. endlesszeal

    endlesszeal Senior Member

    Messages:
    2,670
    Likes Received:
    35
    Joined:
    Nov 13, 2002
  6. reikoshea

    reikoshea HS Troll...And Mod Moderator VIP

    Messages:
    12,633
    Likes Received:
    193
    Joined:
    Apr 27, 2005
    Location:
    San Antonio, TX
    well the biggest bad habit for programmers to kick is inline variable usage.

    I saw it 5-6 times in your examples. its stuff like:

    Code:
    System.out.println( "The value of the investment in 20 years will be " 
                                               + pricePattern.format( value20yrs ));
    
    Now i know you just started the class so im not trying to harass, because i know it will be covered later. But other people end up looking at code when you do this for a living and I generally cringe when i see output done that way.

    1) Don't use functions inside outside of defining a variable

    Code:
    System.out.println( "Your text" + func( var ) );
    
    Define a variable instead.

    Code:
    final=func( var );
    System.out.println( "Your text" + final );
    
    2) MANY places you will work have coding guidelines that forbid you from concating inside of a function:

    Code:
    final=func( var );
    System.out.println( "Your text" + final );
    
    Will be this:

    Code:
    msg="your text";
    result=func( var );
    final=msg + result;
    System.out.println( final );
    
    Now the above is super simplified and the chances of working in an office that has a coding guidelines setup this way is pretty slim, but if you're working for a SAS company it's highly likely. This is implemented so code is easily reusable across assignments. So if you have something more complex then you dont have to duplicate work, you just have to tweak a bit and move the code.

    What is Object Oriented Programming (OOP)? - What OOP is NOT

    Read that too. It's about php but applies to most languages.
     
  7. jamesA

    jamesA Well known pissed off telephone guy VIP

    Messages:
    16,129
    Likes Received:
    1,020
    Joined:
    Aug 10, 2003
    ^ and you were making fun of me for being skinny, nerd. lol
     
  8. Ethan

    Ethan Well-Known Member

    Messages:
    2,945
    Likes Received:
    98
    Joined:
    Jun 30, 2008
    hey sticks why you calling him a nerd? lmao jk
     
  9. reikoshea

    reikoshea HS Troll...And Mod Moderator VIP

    Messages:
    12,633
    Likes Received:
    193
    Joined:
    Apr 27, 2005
    Location:
    San Antonio, TX
    do you really want to go there james? i can make you feel really bad about yourself.
     
  10. Ethan

    Ethan Well-Known Member

    Messages:
    2,945
    Likes Received:
    98
    Joined:
    Jun 30, 2008
    uhhh ohhh here comes another live streaming internet suicide... Hide the pills!
     
  11. Dustin_m

    Dustin_m Active Member

    Messages:
    4,603
    Likes Received:
    16
    Joined:
    May 21, 2003
    Location:
    San Diego, CA
    Thanks. I think I get most of that, but I might have to read through it a couple more times. This class is online, so my only examples are from the book, so that's the reason I've formatted it this way. Do you think this is something that would likely be covered later for some reason, or maybe a crappy book/author?
     
  12. jamesA

    jamesA Well known pissed off telephone guy VIP

    Messages:
    16,129
    Likes Received:
    1,020
    Joined:
    Aug 10, 2003
    lol

    I'm just fuckin around man. I don't know what you'd possibly say, but in the end nothing you could say would affect me at all.


    Dustin: how old is the book you're taking examples from? Have you searched for different authors on the subject to possibly get a different point of view or way of writing the code?
     
  13. TommyTheCat

    TommyTheCat Gonzo Scientist

    Messages:
    1,248
    Likes Received:
    11
    Joined:
    Sep 29, 2002
    Location:
    Minneapolis, MN
  14. D16SiHatch

    D16SiHatch New Member

    Messages:
    1,297
    Likes Received:
    44
    Joined:
    Sep 7, 2007
    this is like exactly the same stuff i was doing in my intro to java programming class a few quarters ago. i did real well while in the class but i feel like now i would be unable to even type up a basic program to save my life lol.
     
Verification:
Draft saved Draft deleted

Share This Page