for loop in java

display a desired output using for loop

Page 1 of 1

5 Replies - 1321 Views - Last Post: 07 December 2010 - 11:55 PM Rate Topic: -----

#1 Guest_lanz*


Reputation:

for loop in java

Posted 06 December 2010 - 10:06 AM

I have a problem on how I will display this design on my screen using for loop statement? I'm working on it for days, but still can't make it..anyone who can share knowledge and give advice?I'll appreciate it a lot.



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


This post has been edited by macosxnerd101: 06 December 2010 - 10:10 AM
Reason for edit:: Added code tags around shape


Is This A Good Question/Topic? 0

Replies To: for loop in java

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,962
  • Joined: 15-July 08

Re: for loop in java

Posted 06 December 2010 - 02:00 PM

If I break it down, will that help?
1st:
          *        
         ***  
        *****       
      *********



Do that part with one for loop.

2nd:
        *****   
   *    *****     * 
  **    *****     **     
 *******     ********    
********     ********* 



See the pattern? You may have to use if statements in this one...

Then do it in reverse!
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: for loop in java

Posted 06 December 2010 - 02:04 PM

Are you allowed to use arrays? Have you been taught about them yet?
Was This Post Helpful? 0
  • +
  • -

#4 jane010794  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 10

Re: for loop in java

Posted 07 December 2010 - 09:34 PM

this one is not an executable class...


/**
*The Fraction class
*A template for a fraction that has the form numerator/denominator
*/
public class Fraction {
// data members
private int numerator; // holds the numerator of this fraction
private int denominator; // holds the denominator of this fraction
/**
*Constructs a fraction with numerator=0 and denominator = 1
*This constructor allows a Fraction with an equivalent numeric value of zero to
*be created… This becomes the default constructor (since it has no arguments)…
* Example of usage: Fraction f = new Fraction();
**/
public Fraction() {
numerator = 0;
denominator = 1;
}
/**
* This constructor creates a Fraction with the specified whole number as its initial value…
* Example of usage: Fraction f = new Fraction(10);
*/
public Fraction(int wholeNumVal) {
this.numerator = wholeNumVal;
denominator = 1;
}
/*
Alternative version of the constructor above, implemented by calling the
constructor defined below, using the this reference variable…
public Fraction(int wholeNumVal) {
this(wholeNumVal, 1);
}
*/
/**
*This constructor creates a Fraction using the explicit numerator and denominator values…
*Example of usage: Fraction f = new Fraction(5, 2);
*/
public Fraction(int numerator, int denominator) {
this.numerator = numerator; // the keyword this may be omitted
this.denominator = denominator; // the keyword this may be omittted
}
/**
*Accessor/Getter Method
*Returns the value of the numerator of this fraction
**/
public int getNumerator(){
return numerator;
}
/**
*Accessor/Getter Method
*Returns the value of the denominator of this fraction
**/
public int getDenominator(){
return denominator;
}
/**
*Mutator/Setter Method
*Sets the value of the numerator of this fraction to n
**/
public void setNumerator(int n){
numerator=n;
}
/**
*Mutator/Setter Method
*Sets the value of the denominator of this fraction to d
**/
public void setDenominator(int d){
denominator=d;
}
/**
*Returns a string form of the fraction following the format numerator/denominator
**/
public String toString(){
if(denominator==1)
	return(numerator + "");
	else
	return (numerator + "/" + denominator );
}
/**
*Returns the reduced(simpliest) form of this fraction
**/
public Fraction reduce(){
Fraction r= new Fraction(); // construct a fraction
int gCF = computeGCF(); // determine the greatest common factor of numerator and denominator
int newN= numerator/gCF; //compute newN, the numerator of the simpliest form of this fraction
//compute newD, the denominator of the simpliest form of this fraction
int newD = denominator/gCF;
r.setNumerator(newN); // set the numerator of the simpliest form to newN
r.setDenominator(newD);// set the denominator of the simpliest form to newD
return r; // return the simpliest form of this fraction
}
// Compuets the greastest common factor of the numerator and denominator
private int computeGCF(){
int gCF=1;
int lesser = 1;
boolean found=false;
lesser = computeLesser(numerator, denominator);
for (int candidate= lesser; (candidate >= 1 && !found); candidate--){
if (numerator%candidate == 0 && denominator%candidate==0){
found = true;
gCF= candidate;
}
}
return gCF;
}
//Returns the lesser integer between n1 and n2
private int computeLesser(int n1, int n2){
int lesser=0;
if (n1<n2)
lesser = n1;
else
lesser = n2;
return lesser;
}
/**
*Returns the sum of this fraction and another fraction g
**/
public Fraction sum(Fraction g){
Fraction s=new Fraction();
int den = denominator * g.getDenominator();
int num = den/denominator*numerator + den/g.getDenominator()*g.getNumerator();
s.setNumerator(num);
s.setDenominator(den);
return s.reduce();
}
/**
*returns the difference of this fraction and another fraction g
**/
public Fraction difference(Fraction g){
Fraction d=new Fraction();
int den = denominator * g.getDenominator();
int num = den/denominator*numerator - den/g.getDenominator()*g.getNumerator();
d.setNumerator(num);
d.setDenominator(den);
return d.reduce();
}
/**
*Return the product of this fraction and another fraction g
**/
public Fraction product(Fraction g){
Fraction p=new Fraction();
int den = denominator * g.getNumerator();
int num = den/denominator*numerator * den/g.getDenominator()*g.getNumerator();
p.setNumerator(num);
p.setDenominator(den);
return p.reduce();
}
/**
*Return the quotient of this fraction and another fraction g
**/
public Fraction quotient(Fraction g){
Fraction q=new Fraction();
int den = denominator * g.getDenominator();
int num = den/denominator*numerator / den/g.getDenominator() *g.getNumerator();
q.setNumerator(num);
q.setDenominator(den);
return q.reduce();
}


} // end of Fraction class

and this one has an error and it is an executable class..

import java.util.Scanner;

public class TestFraction2{
	Scanner scan = new Scanner(System.in);

	public TestFraction2(){

	}

	public static void main(String[] args){

	   int choice;

       System.out.println("This program helps you perform arithmetic operations with fractions");
	   System.out.println("What do you want to do?");
	   System.out.println("1. Add fractions");
	   System.out.println("2. Subtract fractions");
	   System.out.println("3. Multiply fractions");
	   System.out.println("4. Divide fractions");
	   System.out.println("5. Reduce a fraction");
	   System.out.println("6. Quit");

	   do{

	   	System.out.println("Enter your choice<1/2/3/4/5/6>:");
        choice = scan.nextInt();

        switch(choice){
        	case 1:Fraction sum();
        	       System.out.println("Sum = " + s.reduce);
        	       break;

        	case 2:Fraction difference();
        	       System.out.println("Difference = " + d.reduce);
        	       break;

            case 3:Fraction product();
        	       System.out.println("Product = " + p.reduce);
        	       break;

        	case 4:Fraction quotient();
        	       System.out.println("Quotient = " + q.reduce);
        	       break;

            case 5:Fraction reduce();
        	       System.out.println("Reduce = " + r);
        	       break;

        	case 6:System.out.println("Thank you for using my Program!");
        	       System.exit(0);

        default: System.out.println("Invalid choice! Enter again");
        }
	   }while(choice<=7);


		TestFraction2 myProgram;

		try{
			myProgram = new TestFraction2();
			myProgram.run();

		}catch (Exception e){
			System.out.println(e.getMessage());
		}
	}

	public void run(){
	   Fraction f1 = readFraction();
	   Fraction f2 = readFraction();
	   System.out.println(f1.toString());
	   System.out.println(f2);

	}

	public Fraction readFraction(){
		Fraction f;
		System.out.print("Enter a numerator: ");
		int num =  readInt();
		System.out.print("Enter a denominator: ");
		int den =  readInt();
        f = new Fraction(num, den);
		return f;
	}

   public int readInt(){
   	  int value=0;
   	  boolean noProblem = true;
     do {
      try {
   	    value = Integer.parseInt(scan.nextLine());
   	    noProblem = true;
      } catch (Exception e ){
      	System.out.println("You entered an invalid datum. Enter again: ");
      	noProblem = false;
      }

     } while (!noProblem);

   	  return value;
   }


}


it has an error in the switch case like in case 1: ';' expected

hoping can find a better code..

Edited by macosxnerd101: Please, :code:.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: for loop in java

Posted 07 December 2010 - 09:41 PM

A switch statement if constructed at compilation time
so it is built from CONSTANTS

   switch(value) {
      case 1:
        ....
        break;
      case 2:
        ....
        break;



it CANNOT be built at run time using a value returned by a method
    switch(value) {
       case method1();
         ....
         break;
       case method2();
         ....
         break;


Actually think about it.. what will you do if method1() and method2() return the SAME value ???
Was This Post Helpful? 0
  • +
  • -

#6 Guest_lanz*


Reputation:

Re: for loop in java

Posted 07 December 2010 - 11:55 PM

no we haven't learned yet about arrays,that's why we're not allowed to use them..
Was This Post Helpful? 0

Page 1 of 1