Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 12, 2007, 01:01 PM   #1
MBP123
macrumors regular
 
Join Date: May 2006
Simple Java Quest

To print out this output

********
* *
* *
* *
* *

right now i have the top row and the left aligned stars, but cant get the right aligned ones.

Code:
int HorizLine, VertLine;
        
        for (HorizLine=1; HorizLine<=1; HorizLine++)
	   {
		for (VertLine=1; VertLine<=10; VertLine++)
				{
				System.out.print("*");
				}
			System.out.print("");
			}//row of solid asteriks
        
        for (HorizLine=1; HorizLine<=4; HorizLine++)
           {
                for (VertLine=1; VertLine<=1; VertLine++)
                                {
                                System.out.print("\n");
                         System.out.print ("*");//left aligned asteriks
could someone please steer me in the right direction as to how to get them to be in line with the other side? thanks
MBP123 is offline   0 Reply With Quote
Old Nov 12, 2007, 01:02 PM   #2
MBP123
Thread Starter
macrumors regular
 
Join Date: May 2006
well the image didnt display properly in the forum....but its supposed to the one row of stars in line with the first asterisk, and the other line of asterisks in line with the last asterisk of the top row.
MBP123 is offline   0 Reply With Quote
Old Nov 12, 2007, 01:09 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Well, if you can assume fixed width fonts (seems the only way doing what you are doing) I would simply print one "*" then 8 " " then another "*".
robbieduncan is offline   0 Reply With Quote
Old Nov 12, 2007, 01:12 PM   #4
MBP123
Thread Starter
macrumors regular
 
Join Date: May 2006
no its not fixed because the user enters the width by inputting a digit earlier in the program. so it can be anything between 5 and 20.

so its more like

Code:
for (HorizLine=1; HorizLine<=1; HorizLine++)
	   {
		for (VertLine=1; VertLine<=width; VertLine++)
				{
				System.out.print("*");
				}
			System.out.print("");
MBP123 is offline   0 Reply With Quote
Old Nov 12, 2007, 01:38 PM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
OK. If I assume your width is stored in an int called width:

Code:
int width;

<get user to input and validate storing in width>

int i,j;

for (i=0;i<width;i++)
{
System.out.print("*");
}
System.out.print ("\n");

for (j=0;j<4;j+)
{
System.out.print("*");
for (i=0;i<(width-2);i++)
{
System.out.print(" ");
}
System.out.print("*\n");
}
robbieduncan is offline   0 Reply With Quote
Old Nov 12, 2007, 02:14 PM   #6
Gelfin
macrumors 68020
 
Gelfin's Avatar
 
Join Date: Sep 2001
Location: San Francisco, CA
I'm assuming this is a homework assignment, so I'm not going to provide an answer, but here are a couple of hints:

First, learn how to indent correctly. If your instructor isn't knocking at least five points off for that, he should.

Second, if you recall last time you posted a question like this, I replied with an unconscionably smartassed solution that did the bulk of the work on one line. That solution was terrible because it was unreadable and unmaintainable, but the general approach was valid, and since this is a much simpler problem, you'd do well to consider it:

You are writing out a square of characters with known height and width, one coordinate at a time. Treat that as a separate problem. Do your nested loops, and inside them call a method whose job it is to figure out whether or not a star should be written at a given X and Y coordinate. Think about the conditions that make a position a "star" position instead of a "space" position. If your method returns true, write a star. If it returns false, write a space.

In your current problem, the conditions are very simple -- far more so than in that checkerboard+diamond problem. A position should contain a star if it is in the top row, the leftmost column, or the rightmost column (assuming I've understood your assignment correctly). You can easily define a test method for those conditions in a few lines of code.

If you want to really impress your instructor, make a general class that writes out a matrix of stars (define your test method to always return true) then each time he assigns you one of these problems reuse the general class by deriving a subclass that overrides your test method with one that returns the correct values for the assignment.

I mean, don't be surprised if he thereafter gives you an assignment that breaks your base class by asking for a matrix with more than two kinds of character in it. Compensating for that possibility upfront is left as an exercise you may easily be forgiven for not wanting to undertake just now.
__________________
Reality is that which, when you stop believing in it, doesn't go away.
--Philip K. Dick
Gelfin is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM
Pictures on Simple Java GUI won't work. macuser1232 Mac Programming 1 Jun 26, 2012 11:17 PM

Forum Jump

All times are GMT -5. The time now is 09:27 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC