Results 1 to 9 of 9

Thread: help java homework

  1. #1
    Join Date
    Jun 2008
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help java homework

    i wanted the number of asterisk be equal to the number entered by the user. here's my sourcecode...

    Code:
    import java.io.*;
    
    class ForLoopRequirement 
    {
    	public static void main (String [] args) throws Exception
    	{	
    		BufferedReader inputNo= new BufferedReader (new InputStreamReader (System.in));
    
    			int survey1, survey2, x, y;
    			String result1=" ", result2=" ";
    
    
    			System.out.println ("How would you rate your computer programming subject? ");
    			survey1= Integer.parseInt(inputNo.readLine());
    			System.out.println ("How about your instructor? ");
    			survey2 = Integer.parseInt(inputNo.readLine());
    
    
    
    			System.out.println ("Computer Programming:");
    			for(x=0;x<=survey1;x++)
    				{
    				result1=result1+"*";
    				System.out.print (result1);
    				
    				}
    			System.out.println (" ");	
    			System.out.println ("Instructor:");
    			for(y=0; y<=survey2;y++)
    		
    				{
    				result2=result2+"*";
    				System.out.print (result2);
    				
    				
    				}	
    
    	}
     }

    the output will always be :


    How would you rate your computer programming subject?
    5
    How about your instructor?
    6
    Computer Programming:
    * ** *** **** *****
    Instructor:
    * ** *** **** ***** ******



    i wanted it like this
    How would you rate your computer programming subject?
    5
    How about your instructor?
    6
    Computer Programming:
    *****
    Instructor:
    ******

  2. #2
    Join Date
    Jun 2008
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    okay got it..


    import java.io.*;

    class ForLoopRequirement
    {
    public static void main (String [] args) throws Exception
    {
    BufferedReader inputNo= new BufferedReader (new InputStreamReader (System.in));

    int survey1, survey2, x, y;


    System.out.println ("How would you rate your computer programming subject? ");
    survey1= Integer.parseInt(inputNo.readLine());
    System.out.println ("How about your instructor? ");
    survey2 = Integer.parseInt(inputNo.readLine());



    System.out.println ("Computer Programming:");
    for(x=survey1;x>0;x--)
    {

    System.out.print ("*");

    }
    System.out.println (" ");
    System.out.println ("Instructor:");
    for(y=survey2; y>0;y--)

    {

    System.out.print ("*");


    }

    }
    }

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Why do you people have such an unreasonable hatred of putting things into separate functions?
    Code:
    import java.util.Scanner;
    import java.util.InputMismatchException;
    import java.io.InputStream;
    
    public class Rating {
        Scanner sc = null;
        InputStream source = null;
    
        public Rating(InputStream src) {
    	source = src;
    	sc = new Scanner(src);
        }
    
        public static String dup(String str, int n) {
    	StringBuilder ret = new StringBuilder("");
    
    	for (int i = 0; i < n; ++i)
    	    ret.append(str);
    
    	return ret.toString();
        }
    
        public int queryInt(String question) {
    	int r = 0;
    	boolean done = false;
    
    	while (!done) {
    	    System.out.print(question + " ");
    
    	    try {
    		r = sc.nextInt();
    		done = true;
    	    } catch (InputMismatchException e) {
    		System.out.println("Sorry, we couldn't parse that.");
    		sc = new Scanner(source);
    	    }
            }
    
    	return r;
        }
    
        public void showRating(String label, int rating) {
    	System.out.print(label + " ");
    	System.out.println(dup("*", rating));
        }
    
        public static void main(String[] args) {
    	Rating op = new Rating(System.in);
    	int progRating = op.queryInt("How would you rate your computer programming subject?"),
    	    instRating = op.queryInt("And how about your instructor?");
    
    	System.out.println("");
    
    	op.showRating("Computer programming:", progRating);
    	op.showRating("Instructor:          ", instRating);
        }
    }
    public static void main (String [] args) throws Exception
    Don't ever, ever, ever declare something to throw Exception. Doing so renders the whole exception system useless! If at all possible, catch it and deal with it. If not, throw only the exceptions you expect to have to throw.

    Importing * from a package is likewise frowned upon, although understandable if you find yourself importing most or all of the classes from the package anyway, which is most certainly not the case here.

    Haskell equivalent:
    Code:
    import System.IO (hSetBuffering, stdin, stdout, BufferMode(NoBuffering))
    
    askQuestion q = do
        putStr $ q ++ " "
        s <- getLine
        case reads s of
            (v, "") : _ -> return v
            _           -> do putStrLn "Sorry, we couldn't parse that."
                              askQuestion q
    
    showRating l n = putStrLn $ l ++ ' ' : replicate n '*'
    
    main = do
        mapM_ (flip hSetBuffering $ NoBuffering) [stdin, stdout]
        p <- askQuestion "How would you rate your computer programming subject?"
        i <- askQuestion "And your instructor?"
        putStrLn ""
        showRating "Computer Programming:" p
        showRating "Instructor:          " i
    Last edited by Twey; 10-15-2008 at 07:13 AM. Reason: Unnecessary brackets.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. The Following User Says Thank You to Twey For This Useful Post:

    AmenKa (11-08-2008)

  5. #4
    Join Date
    Sep 2008
    Location
    London or Slovakia
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by Twey View Post
    Why do you people have such an unreasonable hatred of putting things into separate functions?
    Unfortunatelly it is the current learning curve at many schools. You are explained the topic in later stage but no real pressure put on use of it

  6. #5
    Join Date
    Jun 2008
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    yeah.. we're still on our early stages of learning java. So my way of presenting the my program is kinda "primitive".

  7. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I maintain my opinion that all students should be taught Scheme first so they can learn to program without a programming language getting in the way... lilyoungfella, if you want to learn how to really program, I recommend going through SICP.
    Last edited by Twey; 10-12-2008 at 05:52 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. The Following User Says Thank You to Twey For This Useful Post:

    AmenKa (11-08-2008)

  9. #7
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Haha, what compelled the Haskell code?

    I like the mapM_, but you don't need the $ for that. You can also avoid parens in your showRating w/o loss of efficiency.
    Code:
    showRating l n = putStrLn $ l ++ " " ++ replicate n '*'
     . . .
    mapM_ (flip hSetBuffering NoBuffering) [stdin, stdout]
    Trinithis

  10. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I make a habit of showing the alternative in a reasonable language whenever I'm asked to write something in a daft one, lest some newbie programmer reads it and thinks that programming mainly consists of writing thousand-line FooFactoryFactoryFactories in order to satisfy bizarre type requirements for a particularly stupid compiler, and gets put off programming altogether.

    As far as I'm aware, a ++ [b] ++ c is considerably less efficient than a ++ b : c — so much so that it's often feasible to reverse a whole list in order to avoid doing it, according to benchmarks by... gwern, I think. The brackets still aren't necessary, though

    You're right, the $ isn't necessary, but it helps clarify it to me. Perhaps I should try to adjust.
    Last edited by Twey; 10-15-2008 at 07:13 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  11. #9
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Guess I should have checked . . . but I find it surprising that the compiler doesn't optimize the concatenation of literals. And yeah, hurray for fixity declarations to solve paren problems!
    Trinithis

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •