Java program that prints 1000 times.

  • Context: Java 
  • Thread starter Thread starter Hiche
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary

Discussion Overview

The discussion revolves around a homework assignment in a beginning Java course, where participants explore methods to print a statement a thousand times without using loops. The focus is on finding the simplest approach using methods and possibly recursion.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant suggests creating a method that prints the statement multiple times and calling it from the main method, questioning if this is the simplest approach given the constraints.
  • Another participant proposes that instead of writing a method with 1000 print statements, one could write a method with fewer statements and call it multiple times, hinting at a multiplication approach.
  • A different participant shares a code snippet that calls a print method multiple times, thanking another participant for their input and expressing uncertainty about the correctness of their solution.
  • One participant introduces the idea of using limited recursion as a method to achieve the goal without loops, suggesting the use of a counter variable to control the recursion.
  • Another participant agrees with the recursion idea but notes that if the original poster (OP) has not studied loops, they may also be unfamiliar with recursion.
  • A final participant provides a code example of recursion, explaining that it involves a function calling itself and demonstrating how to implement it to print a statement a thousand times.

Areas of Agreement / Disagreement

Participants express various methods to solve the problem, including using multiple print statements, recursion, and the potential for confusion regarding loops and recursion. There is no consensus on a single best approach, and multiple competing views remain.

Contextual Notes

Some participants express uncertainty about the OP's familiarity with recursion, suggesting that the discussion may depend on the OP's prior knowledge of programming concepts.

Hiche
Messages
82
Reaction score
0
Okay.

So, I'm taking up a beginning Java course in college and we have been assigned a rather simple (?) homework.

We have to code a program that prints a statement a thousand times, but without the use of loops, only methods. I know I can create a method that contains a number of the statement and then call it several times from the main method, but is there an easier way? Our instructor asked for the simplest way. Is the one I mentioned the simplest way when we are limited to methods?

I'm still new to programming. How did I know about loops? Read a few further chapters ahead of our course.

Code:
public class MyProgram
{
	public static void main(String[] args)
	{
		printThousandTimes();
		printThousandTimes();
                // call some more and more..
		
	}
	
	public static void printStatement
	{
		System.out.println("statement");
	}

	public static void printThousandTimes
	{
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
	}
}
 
Technology news on Phys.org
You don't need to write a method with 1000 identical print statements.

What do you think happens if you write a method with say 5 print statements, and call it 5 times.

Now think about the fact that 1000 = 10 x 10 x 10.
 
Code:
public class MyProgram
{
	public static void main(String[] args)
	{
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
		printThousandTimes();
	}
	
	public static void printStatement()
	{
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
		System.out.println("statement");
	}

	public static void printThousandTimes()
	{
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
		printStatement();
	}
}

Like this?

And thank you, Aleph Zero. Bare with me if my answer is not true, if you may.
 
I think limited recursion would qualify for not being a loop. Just pass a counter variable and don't recurse when that variable exceeds 1000.
 
Hiche said:
Like this?

You got it :smile:

fleem said:
I think limited recursion would qualify for not being a loop. Just pass a counter variable and don't recurse when that variable exceeds 1000.
Maybe so, but if the OP hasn't studied loops yet, he/she probably doesn't know about recursion either.
 
I'm not entirely sure if it could count as a loop, but there is a really simple way to do this without using any "loop" keywords(for, do, while...)
Code:
public class MyProgram
{
    public static void main(String[] args) {
        rec(0);
    }
    
    public static void rec (int count) {
        System.out.println(count); //Or whatever else you wish really...
        if (count<1000) rec(count++);
    }
}
It's called recursion. As in a function that calls itself...
 

Similar threads

Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 5 ·
Replies
5
Views
2K