PDA

View Full Version : help java homework



lilyoungfella
10-11-2008, 02:49 PM
i wanted the number of asterisk be equal to the number entered by the user. here's my sourcecode...



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:
******

lilyoungfella
10-11-2008, 02:52 PM
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 ("*");


}

}
}

Twey
10-11-2008, 08:06 PM
Why do you people have such an unreasonable hatred of putting things into separate functions?
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 ExceptionDon'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:
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

peter_budo
10-12-2008, 08:17 AM
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

lilyoungfella
10-12-2008, 11:12 AM
yeah.. we're still on our early stages of learning java. So my way of presenting the my program is kinda "primitive".

Twey
10-12-2008, 05:29 PM
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 (http://mitpress.mit.edu/sicp/).

Trinithis
10-13-2008, 11:58 PM
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.


showRating l n = putStrLn $ l ++ " " ++ replicate n '*'
. . .
mapM_ (flip hSetBuffering NoBuffering) [stdin, stdout]

Twey
10-15-2008, 08:06 AM
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.

Trinithis
10-15-2008, 06:11 PM
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!