GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2005, 02:29
Skyer Skyer is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Skyer is on a distinguished road

My first Java Program (Desperate for Help)


Hi,
This is one of the examples of the java program that im trying to write. I am very new to this, can anyone help me with this java program, i cant get my head around this, any help would be very appriciated. Thanks.

Here is the question:
Write a program in java, using loops (this program may require
multiple loops) to print the following pattern of stars: (i cant get the shape in here right but it needs to be in a diamond shape not triangle)
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
There are 1, 3, 5, 7, 9, 9, 7, 5, 3 and 1 stars in each of the corresponding
rows.
  #2  
Old 08-Oct-2005, 05:08
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: My first Java Program (Desperate for Help)


hi Skyer..

Welcome to GIDForums..

You have almost done your program except that:
You have to print spaces(blank character) before you print * in every line.

so design a for loop where you can print spaces as required so that you may obtain the diamond shape.

so for the first line, you should print (n-1)/2 number of spaces and so on
where n is the number of lines you want to print whether it is even or odd.

Bye,
Paramesh
  #3  
Old 09-Oct-2005, 02:20
Skyer Skyer is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Skyer is on a distinguished road

Re: My first Java Program (Desperate for Help)


I am very new to this can anyone give me the code to get that diamond shape pls? Thanks....
  #4  
Old 09-Oct-2005, 06:03
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: My first Java Program (Desperate for Help)


hi Skyer,

Here is the algorithm i have used for the program:

1. The program is divided into three parts.

-> one part to print the top portion of the program,
-> one to print the middle line of the program(if the number of lines to be printed is odd)
-> and the last part to print the bottom half of the program.

2. To print the top and bottom portion:
---> You have to print n/2 lines. make a variable i to vary from 0 to n/2 in a loop.
---> In each line you have to print spaces and asterisk. So we need two loops each.


3. To print the middle line, we must print n asterisks. thats all.
This must be printed only if n is odd.

Here is the program.

This program works with both odd and even number of lines.
n is the number of lines to be printed.
You can vary n and see the output accordingly.

Please read the comments at the end of every line for understanding the program.
JAVA Code:
class diamond                                     //class diamond
{
   public static void main( String args[] )       //main method
   {
	int  i , j , k, n = 24;                   //n is the number of lines and i,j are used for loops

	k=(n%2==0)? n-1:n;                        //k is used to control the number of spaces to be printed

	for(  i = 0  ;  i < n/2  ;  i++ )         //for the top portion of the diamond
	{
             for( j = 0 ; j < k / 2 - i ; j++ )   //print required spaces
                 System.out.print(" ");

             for ( j = 0 ; j < i * 2 + 1 ; j++ )  //print required stars
 	           System.out.print("*");

             System.out.println();                //line break after the end of a line
        }

        if( n % 2 != 0)                           //if the number of lines is odd
        {
           for( i = 0 ; i < n ; i++ )             //print extra asterisks in the middle
                System.out.print("*");

           System.out.println();                  //line break
        }

        for( i = n/2-1  ;  i >= 0  ;  i-- )       //for the bottom portion of the diamond
	{
             for( j = 0 ; j < k/2 - i ; j++ )     //print required spaces
                 System.out.print(" ");

             for( j = 0 ; j < i*2+1 ; j++ )       //print required stars
                 System.out.print("*");

             System.out.println();                //line break after the end of a line
        }
    }                                             //end main
}                                                 //end class diamond

If you have any doubts, please post again to clarify.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 10-Oct-2005, 04:22
Skyer Skyer is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Skyer is on a distinguished road

Re: My first Java Program (Desperate for Help)


Hey Paramesh,
Thanks alot, that was very useful
Cheers
  #6  
Old 28-Oct-2006, 17:14
gujuchic gujuchic is offline
New Member
 
Join Date: Oct 2006
Posts: 23
gujuchic has a little shameless behaviour in the past

Re: My first Java Program (Desperate for Help)


hey paramesh, i need your help...I dont know wht is the code for the "send" button in my prog....i have a prog where i have to write a prog for an email message..i have the text boxes but dunno wht to put the code for "send " button...if u can tell what code to use it will be helpful.....
  #7  
Old 30-Oct-2006, 08:39
gujuchic gujuchic is offline
New Member
 
Join Date: Oct 2006
Posts: 23
gujuchic has a little shameless behaviour in the past

Re: My first Java Program (Desperate for Help)


hey Paramesh,

I have listed the code info under java...
  #8  
Old 04-Nov-2006, 15:45
Justin Fox Justin Fox is offline
Junior Member
 
Join Date: Sep 2006
Posts: 46
Justin Fox is on a distinguished road

Re: My first Java Program (Desperate for Help)


for a button...

JAVA Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Email extends JPanel
{

JButton button;

public JPanel()
{

setLayout(null)
setBackground(Color.green);
setPreferredSize(new Dimension(300,300));

button = new JButton("Send");
button.setBounds(100,150,50,20);
button.setToolTextTip("send the email");

add(button);

button.addActionListener(new buttonListener());
}

public class buttonListener implements ActionListener
{
    public void actionPerformed( ActionEvent event)
    {
       if(event.getSource() == button)
       {
       // do what you want
       }
    }
}

public static void main(String [] args)
{
      JFrame frame = new JFrame("Button Example");
      frame.getContentPane().add(new Email());
      frame.pack()
      frame.setVisible(true);
      frame.setDefaultCloseOperation(3);
}

}



hope this helps you see how to add an action listener, and differiciate between them by just adding the same actionlistener to the different
buttons...


any questions reply

Justin
  #9  
Old 09-Nov-2006, 11:38
gujuchic gujuchic is offline
New Member
 
Join Date: Oct 2006
Posts: 23
gujuchic has a little shameless behaviour in the past

Re: My first Java Program (Desperate for Help)


thanks justin......i wanted to ask u somthing esle too...how u write the code for J panel?????and wht are components in the program,..ne examples????
  #10  
Old 14-Nov-2006, 15:00
Justin Fox Justin Fox is offline
Junior Member
 
Join Date: Sep 2006
Posts: 46
Justin Fox is on a distinguished road

Re: My first Java Program (Desperate for Help)


umm, I dont fully understand, I wrote an example of a JPanel above,
and the components in a JPanel can be anything...

Images,JButtons,additional JPanels, JFrames(for popups),JTextFields,JTextAreas,etc...


Justin

go to sun.com and look at the api for java 1.5.0 and you can look up
component and you'll find everything...
__________________
Justin
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
Made program in Java, trying C++ now, file i/o problems technickel C++ Forum 4 19-Feb-2005 00:32
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
calling a java program from C Magicman C Programming Language 3 20-Oct-2004 07:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.