PDA

View Full Version : Need Help Writing This is Java



BSill
09-19-2006, 10:05 PM
Hello,

I'm new to Java altogether and eager to learn it, mainly for my major. Ive purchased a book from amazon.com titled Java 5. In this book it explains in paragraph form how to code in Java but it does not show you what the program looks like in code, only states "try to do this and try to do that." If anybody knows JAVA can you please do these for me so I can actually see what the finished product looks like, thank you very much!

- Write an application program that prints your name and
your major in a box of stars on two separate lines. Example:

********************
* NELI ZLATAREVA *
* Computer Science *
********************

- Write an application program that prints AAAAAAA BBBBBBB
CCCCCCC in three different ways:

a) on one line
b) on three lines
c) inside a box made up of the characters * and =

- Write an application program that converts miles to
kilometers. 1 mile = 1.60935 kilometers. Read the miles value from
the user as a floating point value.

Magnj
09-19-2006, 10:24 PM
Dude, go to class next time instead of trying to get other people to do it at 2am the night before class.

bullet2urbrain
09-20-2006, 06:57 AM
your goign to want to use Print, println and scanner.

BSill
09-20-2006, 07:11 AM
Magnj it is nothing that has to be turned in, its a practice lab. Not graded so im not worried about figuring it out asap.

After reading more I found its alot like VB and c++ but variables have to be labeled. The comments thing just confused me why they say to put comments throughout the coding lines instead of just at the top.

eshbach
09-20-2006, 01:12 PM
Since I have nothing better to do, here is a very nice solution to all three problems:




public class BSillsClass
{
public static void main(String[] args)
{
doPartA();
doPartB();
doPartC();
}

private static void doPartA()
{
printRowOfChar('*', 20);
printWordInChar("NELI ZLATAREVA", '*', 20);
printWordInChar("Computer Science", '*', 20);
printRowOfChar('*', 20);
System.out.println();
}

private static void doPartB()
{
System.out.println("AAAAAAA BBBBBBB CCCCCCC");
System.out.println("AAAAAAA");
System.out.println("BBBBBBB");
System.out.println("CCCCCCC");
printRowOfChar('*', 20);
printWordInChar("AAAAAAA", '*', 20);
printWordInChar("BBBBBBB", '*', 20);
printWordInChar("CCCCCCC", '*', 20);
printRowOfChar('*', 20);
printRowOfChar('=', 20);
printWordInChar("AAAAAAA", '=', 20);
printWordInChar("BBBBBBB", '=', 20);
printWordInChar("CCCCCCC", '=', 20);
printRowOfChar('=', 20);
System.out.println();
}

private static void doPartC()
{
java.io.BufferedReader console = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
boolean successfulRead = false;
float userInput = 0.0F;
while (!successfulRead)
{
try
{
System.out.print("Enter a Number of Miles: ");
userInput = Float.parseFloat(console.readLine());
successfulRead = true;
}
catch (java.lang.Exception e)
{
System.out.println(e.getMessage());
System.out.println("Please try again...");
successfulRead = false;
}
}
System.out.println(userInput + " miles = " + userInput * 1.60935 + " kilometers");
}

private static void printRowOfChar(char c, int length)
{
for (int i = 0; i < length; ++i)
{
System.out.print(c);
}
System.out.println();
}

private static void printWordInChar(String word, char c, int length)
{
StringBuffer line = new StringBuffer(20);
int padding;
line.append(c);
if (word.length() > length)
{
padding = 0;
}
else
{
padding = (length - 2 - word.length()) / 2;
}
for (int i = 0; i < padding; ++i)
{
line.append(" ");
}
line.append(word);
while (line.length() < line.capacity() - 1)
{
line.append(" ");
}
line.append(c);
System.out.println(line.toString());
}
}





and here is the produced output:

http://www.baughman-eshbach.com/temp/joutput.jpg



edit: PS, Scanner is for losers, InputStreamReader for life ;)

bullet2urbrain
09-20-2006, 02:46 PM
edit: PS, Scanner is for losers, InputStreamReader for life ;)

Yeah I agree, but keep in mind you've had to help me with Java before too.
Im no where near the level that you are in it, however if you wanna have a assembler code off with a PIC Microcontroller Im game. ;)

eshbach
09-20-2006, 04:47 PM
Yeah I agree, but keep in mind you've had to help me with Java before too.
Im no where near the level that you are in it, however if you wanna have a assembler code off with a PIC Microcontroller Im game. ;)

heh, i was just messing with you... and i've never done anything in PIC assembly (though i've dabbled in X86 and i've had a full class in MIPS assembly).

to Bsill (and anyone else who may be curious): there's nothing wrong with scanner for basic i/o. it's just that those of us who were doing input before java 1.5 like our streams :) also, you'll find that streams give you more flexibility with how you handle the input. you can read a character at a time, or a line at a time, or a certain number of bytes at a time, etc.

but if you're just reading simple user input, there's no reason not to use scanner. it's there for your convenience :)

chron
09-20-2006, 05:04 PM
Reading the chapters and listening to the lecture usually helps. This looks like first day of class type of homework. You really expect to learn anything by asking others to do it for you? Thats impressive...

ahmad
09-20-2006, 06:28 PM
I don't understand. You use a StringBuffer in printWordinChar, yet you fail to do that in printRowOfChar.


private static void printRowOfChar(char c, int length)
{
for (int i = 0; i < length; ++i)
{
System.out.print(c);
}
System.out.println();
}

This has a lot of unnecessary overhead... I think it would be much better to write it like this:


private static void printRowOfChar(char c, int length)
{
StringBuffer t = new StringBuffer();

while (t.length() <= length) {
t.append(c);
}

System.out.println(t);
}

Better yet, I'd rather return a String, and just have one or two printlns. Anyways, I think your solution is good enough for our friend here.

eshbach
09-20-2006, 07:57 PM
I don't understand. You use a StringBuffer in printWordinChar, yet you fail to do that in printRowOfChar.


private static void printRowOfChar(char c, int length)
{
for (int i = 0; i < length; ++i)
{
System.out.print(c);
}
System.out.println();
}

This has a lot of unnecessary overhead... I think it would be much better to write it like this:


private static void printRowOfChar(char c, int length)
{
StringBuffer t = new StringBuffer();

while (t.length() <= length) {
t.append(c);
}

System.out.println(t);
}

Better yet, I'd rather return a String, and just have one or two printlns. Anyways, I think your solution is good enough for our friend here.

yea, that's certainly better. for consistency it would be t.length() < length, but other than that, your way is "more correct".

i was just coding this "stream of consciousness" style :doh: it's not as though this is a program that needed to be efficient or anything ;)

but you're right, if we're going to be teaching by example, we should be doing examples in the best possible way and not just whatever way pops into our heads first.

also, i was thinking.... maybe for the sake of "teaching", i should have made it more object oriented... how about an abstract class "AbstarctLineOfChars" and then "LineOfStars" and "LineOfEquals" that extend it. and maybe it should append to the string buffer recursively, just for academics :p:

we could really go all out on over-engineering a day-1 hoemwork assignment...

ahmad
09-21-2006, 08:48 AM
t.length() < length is only 19 characters :)


also, i was thinking.... maybe for the sake of "teaching", i should have made it more object oriented... how about an abstract class "AbstarctLineOfChars" and then "LineOfStars" and "LineOfEquals" that extend it. and maybe it should append to the string buffer recursively, just for academics :p:

we could really go all out on over-engineering a day-1 hoemwork assignment...

Heh. I think thats a little extreme for a plain output assignment LOL. Maybe use some coding patterns while you are at it :D

Shaolin3a
09-21-2006, 06:56 PM
I'm not new to JAVA but nowhere near being at levels you guys are at. Can one of you ahmad or eshbach explain why the StringBuffer version is better than just a bunch of print statements? I would've thought creating an object and then doing those appends would be more costly.

eshbach
09-21-2006, 07:05 PM
t.length() < length is only 19 characters :)



Heh. I think thats a little extreme for a plain output assignment LOL. Maybe use some coding patterns while you are at it :D

length is 20. t.length has a default value of 0. then, when length is 19 t.length() < length will return true and the loop will append one more time, taking the length up to 20. then t.length < length will return false. if you did <=, when you had t.length = 20, you'd loop through one more time, and end up with 21 characters. :)



I'm not new to JAVA but nowhere near being at levels you guys are at. Can one of you ahmad or eshbach explain why the StringBuffer version is better than just a bunch of print statements? I would've thought creating an object and then doing those appends would be more costly.


StringBuffer is a very efficient class. Since strings in java are immutable, you have to basically have to go through the cost of making a new object every time you change a string. if you use a string buffer, you are just modifying an existing object.

the reason it's better to use a string buffer instead of print statements is that the system overhead in writing to the output stream is pretty high. any time you make a call to system.out you have to wait on the OS. most high level languages do not write directly to the screen.

Shaolin3a
09-21-2006, 07:10 PM
StringBuffer is a very efficient class. Since strings in java are immutable, you have to basically have to go through the cost of making a new object every time you change a string. if you use a string buffer, you are just modifying an existing object.

the reason it's better to use a string buffer instead of print statements is that the system overhead in writing to the output stream is pretty high. any time you make a call to system.out you have to wait on the OS. most high level languages do not write directly to the screen.
Yeah I knew about the immutable object creations. But certainly didn't know about the stream issue. Would this be the same in C/C++ then and writing to files/other streams?

eshbach
09-21-2006, 07:57 PM
Yeah I knew about the immutable object creations. But certainly didn't know about the stream issue. Would this be the same in C/C++ then and writing to files/other streams?

In general, it is good practice to buffer your streams. you don't generally want to write large amounts of data at once, but you do want to minimize the amount of overhead. so you make a buffer of x bytes (or whatever), fill that up, and write that out, repeating until you're done.

Shaolin3a
09-21-2006, 08:03 PM
In general, it is good practice to buffer your streams. you don't generally want to write large amounts of data at once, but you do want to minimize the amount of overhead. so you make a buffer of x bytes (or whatever), fill that up, and write that out, repeating until you're done.

Ah, got it. Thanks for that, it's good to know ;)

ahmad
09-22-2006, 03:49 AM
Thanks for the correction esh. Gotta remember when that check is done.

There is also another benefit to using StringBuffers, and that is they are thread safe. Meaning, you don't run into problems in multithreaded environments unlike doing the String + operation (which is not). Using the string concatenation in a loop in production code is not healthy.

For the most part, you are writing single threaded apps, so this wouldn't be a big deal to you. However when you are writing code that is going to plugged into a larger system, chances are it will be multithreaded and thats when you need to worry about such details.

nfm
09-24-2006, 11:46 AM
How can be eshbach's code wrapped into Windows GUI?