I hope Java looks tough only in the beginning
Hi,
I'm a very beginner in Java programming. As a result I'm using BlueJ appl for writing codes and executing. I know how to execute when my method is of a "main" type. However when the methode is something like "public void show(int[]tab)" I don't really have an idea how to execute this program in order it showed me something using BueJ. I would really appreciate some help.
Thanks
P.S. I'm sorry I'm not yet familiar with the precision of programming questions so it might be a little ambigue for you.
Every program has to have a main method that is where the program starts. For there you can call your show method, etc.
It's nowhere near as hard as it looks, and at the same time it's much harder than any of us would like sometimes. One thing I have learned over the years of working with Java is that if what I'm doing seems really hard, I'm probably doing it wrong.
As another poster commented you have to have an entry point to your application. However, if you're dealing with a web application (which you aren't yet) it won't have a main method. So to blanketly say that you must have a main method is a little inaccurate.
Essentially you have a class that class has methods that it exposes to outside callers.
package com.fw.area51.testing.randomizer;
import java.util.Random;
public class RandomTest
{
public static void main(String[] args)
{
Randomizer rand = new Randomizer() ;
for(int i=0; i<100; i++)
{
System.out.println("For iteration (" + i + ") the value = " + rand.nextInt(100)) ;
}
}
}
class Randomizer
{
private Random r ;
public Randomizer()
{
super() ;
r = new Random() ;
}
public int nextInt(int bound)
{
return r.nextInt(bound) ;
}
}
In this example, the main method of the class RandomTest creates an instance of Randomizer by calling its constructor Randomizer rand = new Randomizer()
and then makes 100 calls to its nextInt(int)
method to get 100 random numbers between 0 and 99 ... This is not the best way to generate random numbers but it illustrates how to call a method of a class.
Make sense?
PS
Thank you so much for the advises and hints.
My code (or whatever I've copied in the class) looks like this:
import java.util.*;
import java.io.*;
public class Table {
private ArrayList numbers;
public void show(int[]tab){
int n;
n=tab[0];
System.out.println(n);
for (int i=1; i<7; i++) {
if(tab!=n){
n=tab;
System.out.println(n);
}
}
}
}
I have a feeling that it doesn't make much sense. I'd rather try using your advise and writing something that shows the list of numbers without any repetitions when the initial list is 3,1,7,3,4,2,7,5,1,0.Our teacher suggested introducing booleans. I've already read a lot about primitive data types and about control flow statements however I'm not able to picture how all this works yet. I keep getting the ideas though:)
Dear puckstooper31 thanks a lot for the "randomizer". Seeing it executed made me smile like a new toy makes smile a kid. Would you guys mind giving me another simple example so I could make some generalisations for myself(in my own words and visions) about java principes . Thanks:)
Here's a more complex example. This creates a window on the desktop centers it and draws some buttons on the window and has the buttons do things when they're clicked.
package com.fw.area51.testing.swingbuttontest;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingButtonTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
initGUI() ;
}
});
}
public static void initGUI()
{
SBTFrame frame = new SBTFrame() ;
frame.centerAndShow() ;
}
}
class SBTFrame extends JFrame
{
private int currentValue ;
private JPanel panel ;
private JButton[] buttons ;
private JTextField txtCurrentValue ;
public SBTFrame()
{
super() ;
init() ;
initComponents() ;
}
public void init()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) ;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage()) ;
}
setTitle("Swing Button Test") ;
getContentPane().setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void initComponents()
{
buttons = new JButton[4] ;
panel = new JPanel(new FlowLayout()) ;
panel.setPreferredSize(new Dimension(250, 50));
for(int i=0; i<buttons.length; i++)
{
buttons[i] = new JButton(String.valueOf(i+1)) ;
buttons[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
handleButtonClick(Integer.parseInt(((JButton) evt.getSource()).getText())) ;
}
});
panel.add(buttons[i]) ;
}
txtCurrentValue = new JTextField("0000") ;
panel.add(txtCurrentValue) ;
getContentPane().add(panel, BorderLayout.CENTER) ;
pack() ;
}
private void handleButtonClick(int buttonValue)
{
currentValue += buttonValue ;
txtCurrentValue.setText(String.valueOf(currentValue));
buttons[0].setEnabled(true);
buttons[1].setEnabled(true);
buttons[2].setEnabled(true);
buttons[3].setEnabled(true);
if(currentValue % 4 == 0)
{
buttons[3].setEnabled(false);
}
if(currentValue % 3 == 0)
{
buttons[2].setEnabled(false);
}
if(currentValue % 2 == 0)
{
buttons[1].setEnabled(false);
}
}
public void centerAndShow()
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize() ;
int xPos = (int) (d.getWidth() - getWidth())/2 ;
int yPos = (int) (d.getHeight() - getHeight())/2 ;
setLocation(new Point(xPos, yPos)) ;
setVisible(true);
}
}
Considering what it does it's actually very little code and if you trace what is calling what it illustrates the interactions between the various classes represented here.
*NOTE* Not all of the buttons actually do anything.
Have fun,
PS
Message was edited by: PS to show a better example than the one he originally included.
puckstopper31>
I did enjoy the program:) drawing buttons on the window. Thanks a lot!
I will get there as well in ... amount of time:)
For instance would anybody be able to help me out with a simple thing like a display of a list of numbers excluding the ones that are repeted. The list would be 3,1,7,3,4,2,7,5,1,0. I have to make my program display :3,1,7,4,2,5,0. I've been trying entire evening to do this.
how do you intend to input the values? are you reading them in from the keyboard? You also need an algorithm that removes the duplicates, so you'll need to add another method to your existing class..
Well,
what would be your advise for me on the way of inputing the values? I've been thinking about variables something like:
int []tab;
i=0, j=0, l=tab2.length-1 and an algorithm which woud be :
while
(i<l){
while(( tab2=tab2[j])&&(tab2[j]=tab2[j- -]!); {
j++;
}
} i++;
System.out.println (tab[j]);
}
The question asking for the program code which displays a list without duplicates was presented to me just like I've posted it in the forum without any further precision. I guess all the possible answers are welcome.
Probably one can tell from the way I'm thinking above that I have a very little idea of how I'm supposed to do it.>
Use the existing data structures to avoid working too much ;-) :
// assumes you get the input numbers as program parameters
public class RemoveDuplicates {
public static void main(String... args) throws NumberFormatException {
Set<Integer> set = new LinkedHashSet<Integer>();
for (String arg : args) {
set.add(Integer.valueOf(arg)); // throws an exception if arg is not an integer
}
System.out.println(set);
}
}
Thank you very much torajirou:)I've been wandering at the same time about how does one evaluate the efficency of the code ? because for me all the codes seem equaly complicated at the moment. how is one able to tell that this code/way"x" is better than"y"?
> How is one able to tell that this code/way"x"
> is better than"y"?
Practice. Writing a lot of crappy code is the only way to learn what's good and what's bad. You'll know when you write something clever (warning: the hit from doing this is seriously addictive). Get it working first; worry about elegance and technique later.
> Practice. Writing a lot of crappy code is the only
> way to learn what's good and what's bad. You'll know
> when you write something clever (warning: the hit
> from doing this is seriously addictive). Get it
> working first; worry about elegance and technique later.
Full ack. I started the OOP way about 10 years ago and at first didn't really get it -- until I created a little GUI using C and felt uneasy about maintaining it a few weeks later. The same applies to today: if I look at code I wrote some months ago, I feel evolution working... Remember: many roads lead to Rome.