Results 1 to 10 of 10
  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Help with asterisk program

    Java Code:
    import java.util.Scanner;
    public class Test {
    	public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	System.out.println("Enter height and width");
    	int height = in.nextInt();
    	int width = in.nextInt();
    	
    	
    	}
    	
    	public static void produceOutput( int height, int width){
    		   for(int i = 0; i < height; ++i){
    			for(int j = 0; j < width; ++j){
    				if (i ==j || j == Math.min(height, width) - j - 1){
    					System.out.print(" ");
    					}
    					else{
    						System.out.print("*");
    						}
    					}
    					System.out.println();
    			}
    			
    	}
    }
    What do I need to get my main method to call the produceOutput method and print the block of asterisks that I want it to print?
    What it does is, you put in a number for height and width, it creates asterisks using those parameters, and creates a big X through the box (the x being spaces).

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with asterisk program

    So you need to call the produceOutput method. If you do not know how to do this then review your lecture notes, textbook or google a tutorial.

  3. #3
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Smile Re: Help with asterisk program

    from your main argument, call produceOutput with the height and width variables you just got.
    So:
    At the end of your main method you need, "produceOutput(height, width)"
    Remember, only what you have in the main method will happen. If you want any other methods you create to run, you have to call them from the main method (or from a method called by the main method, etc... you get the point?)

  4. #4
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with asterisk program

    I know how to call a method...
    you simply put the nameOfMethod(parameter values);
    But I thought you could only do that if there is a return value? And I remember him saying they are both void...unless I am mistaken.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with asterisk program

    Calling methods has nothing to do with return values. If it did then void methods would not be possible.

  6. #6
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with asterisk program

    Okay I will try that. I also need help with another program...
    I created a 2d array. and I want to return two 1d arrays where one returns the averages of the rows in the 2d array, and one returns the averages of the columns. I know what to do in English, but in java it is a bit more complicated..
    Java Code:
    public class TwoDaverage{
    	public static void main(String[] args){
    	double[][] average = {{2,5,7},{6,4,8},{1,3,9}};
    	
    	System.out.println(rowAverage(average));
    	System.out.println(columnAverage(average));
    	}
    	public static double[] rowAverage(double[][] row){
    	double averageOfRows[] = new double[row.length()];
    	for (int i = 0; i < row.lenght; i++){
    
    
    	}
    	
    	
    	return double averageOfRows;
    	}
    }
    After the for loop I have no clue what goes where, brain just shuts.
    I know I need to go through each column and add them together in each row (so maybe a nested for loop?) Any tips?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with asterisk program

    You could write another method that takes a 1D int array and returns the sum of all the numbers in the array. Then your 2 new methods would call that method and pass each row/column in turn and sum the return values. Calculating the average should be simple after that.

  8. #8
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with asterisk program

    No I have to use only two methods..one for the average of row and one for column.
    How about this, I use i and j in nested for loops.
    for (int i =0; i < 3; i++){
    for (int j = 0; j < 3; j++)
    }
    int sum[i] += row[i][j];
    int average = sum/3;

    or would this add all of the values?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help with asterisk program

    Quote Originally Posted by Aimforthehead View Post
    No I have to use only two methods
    I find it hard to believe that you would not be allowed to write as many helper methods as you need.

  10. #10
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with asterisk program

    I found it hard to believe my professor expected us to complete 3 programs on new material with only a few days before a midterm on completely different material. Yet here I am.
    He tells us how many methods to use. What to call them. How many parameters to use. Etc.
    (You'd think being this exact would make the programs easier, but it really doesn't).
    Update. I am working on the row average for now...i've got this
    Java Code:
    public class TwoDaverage{
    	public static void main(String[] args){
    	double[][] average = {{2,5,7},{6,4,8},{1,3,9}};
    	
    	System.out.println(rowAverage(average));
    	
    	}
    	public static double[] rowAverage(double[][] row){
    	double[] sum = new double[3];
    	double j;
    	double i;
    	double[] average;
    	double averageOfRows[] = new double[row.length];
    	for (i =0; i < 3; i++){
    		for (j = 0; j < 3; j++){
    		}
    		sum[i] += row[i][j];
    		average[i] = (sum[i] / 3);
        
    	}
    	
    	
    	return average;
    	}
    }
    Anywhere near close?
    Last edited by Aimforthehead; 11-02-2011 at 04:50 AM.

Similar Threads

  1. Java Asterisk Tree
    By Stratgtar565 in forum New To Java
    Replies: 2
    Last Post: 10-12-2011, 06:13 AM
  2. How to replace int with asterisk
    By erin.ctm in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:13 AM
  3. Using an asterisk to split a string.
    By Cruncher in forum New To Java
    Replies: 2
    Last Post: 06-13-2010, 04:12 PM
  4. asterisk java
    By abhi in forum New To Java
    Replies: 4
    Last Post: 01-22-2008, 08:09 AM
  5. Asterisk-Java 0.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-02-2007, 09:30 AM

Posting Permissions

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