Question:
First time writing java programs?
anonymous
2010-12-01 19:39:20 UTC
I am working on my first two programs for java and was wondering if anyone could take a look at my code to see if I have made any errors. Any advice would be great.



Question2.java
Java program for question 2

Public class Question2()
{
final int N = 20;
static Scanner ins = new Scanner(System.in);

static int main(String [] args)
{
int array (N);
int product


for (int k =0; k <= N; k++)
array (k) = ins.nextInt();

// function call
product = arrayProduct (int array, N);
}

//function definition
void arrayProduct(int array, int N);
{
float product;
float k;

product = 0;
for (k=0; k > N; k++)
System.out.println ( “processing element ” + k +);
product = product * array (k);

return product;
}
}


and also this one


public class RTwo extends JFrame
{



public RTwoProgram()
{
private JLabel length, width, area;
setTitle("Good day area");

length = JLabel("Enter the length);

width = JLabel("Enter the width);

area = JLabel("Area: ");

containerPane = ContentPane();

pane.setLayout(GridLayout(4, 1));

setSize(WIDTH, HEIGHT);
setVisible();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main( String args[] )
{
RTwoProgram R2 = new RTwoProgram ();

}
}
Four answers:
Tony
2010-12-01 19:59:36 UTC
There were many problems with this code, it should look like this:



Question2.java

Java program for question 2



import java.util.Scanner;

public class Question2{

final static int N = 20;

static Scanner ins = new Scanner(System.in);



public static void main(String [] args)

{

int[] array = new int[N];

int product;





for (int k =0; k < N; k++)

array [k] = ins.nextInt();



// function call

product = arrayProduct(array);

System.out.println(product);

}



//function definition

static int arrayProduct(int[] array)

{

int product;

int k;



product = 1;

for (k=0; k < N; k++){

System.out.println ( "processing element" + k );

product = product * array [(int)(k)];

}

return product;

}

}





I wasn't sure what you wanted the second one to do, but it should look basically like this:



import java.awt.*;

import javax.swing.*;

public class RTwo extends JFrame

{







public RTwo()

{

JFrame frame = new JFrame();

JLabel length, width, area;

frame.setTitle("Good day area");



length = new JLabel("Enter the length");



width = new JLabel("Enter the width");



area = new JLabel("Area: ");



Container containerPane = frame.getContentPane();



frame.setLayout(new GridLayout(4, 1));



frame.setSize(200, 200);

frame.setLocationRelativeTo(null);

frame.add(length);

frame.add(width);

frame.add(area);

frame.setVisible(true);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

}



public static void main( String args[] )

{

RTwo R2 = new RTwo ();



}

}
Steven Jeffries
2010-12-01 20:03:18 UTC
Ok, for the first program:

-You should either declare N static, or move all your variables inside the main method

-Main method should be public static void main(String[] args)

-Arrays in Java are declared a bit different than other languages, use int array[] = new int[N];

-(Don't forget your semicolons like after declaring product)

-Again, arrays are accessed by "[]" not "()"

-Your product call to arrayProduct is correct, however you should change the return type from void (which returns nothing) to int (which will return an int)

-Also, there should not be a semicolon after a method declaration

-This for loop will never execute since N is final, and 20, k starts at 0, 0 is never > 20 "

for (k=0; k > N; k++) " it should be k < N

-There should not be a + after k in your System.out.println

-Also, you set product to 0 before the loop, every time you go through the loop, you are saying product = 0 * array[k]; in other words, always 0, initialize at 1



Alright, second program:

-The labels length, width, and area are all inside of a constructor, so they can not have any access modifiers (like private). Just say JLabel length, width, height;

-I really don't know what this is about.... "containerPane = ContentPane();"

-I think you were trying to add the items to the JFrame. Do "getContentPane().add(length);" etc

-Instead of pane.setLayout, since your class extends JFrame, just call setLayout(LayoutManager)

-When creating a new instance of something, use the new operator. setLayout(new GridLayout(4,1))

-WIDTH and HEIGHT wouldn't throw an error because they are static fields in JFrame. You should give it your own width and height, like setSize(400, 400);

-setVisible needs a boolean argument, true to be visible, false to not be visible
Mr. Akukibara
2010-12-01 19:59:53 UTC
I think you shouldn't declare your class this way ==> Public class Question2()

Make it this way ==> public class Question2

Try to make your main method declared this way ==> public static void main(String[] args)



You should know it when you try to compile it.
toda
2016-10-04 05:33:21 UTC
Use Eclipse IDE to run methods. I propose it since it is helping you attention at the software layout instead than at the syntax (you be taught syntax as you software). JDK is wherein you write the methods(akin to Eclipse). You collect it through making definite you have got JRE (java runtime atmosphere)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...