Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. So I need to write a code in netbeans that does the following:

1. Asks the user for the length of the side of a square
2. Invoke a functional method named calculateArea that returns the area of the square
3. Invoke another method that does the same thing for the perimeter
4. Shows both results in one message box

Thank you so much!

What I have tried:

I put:

/*
*
*/

import javax.swing.JOptionPane;

public class Calculate {
public static void main(String[ ] args) {

}

I'm really not sure what I'm doing here
Posted
Updated 15-Mar-16 5:25am
Comments
Patrice T 14-Mar-16 18:27pm    
Look like HomeWork.
It have to be done by you.
Study your course.
Sergey Alexandrovich Kryukov 14-Mar-16 22:58pm    
Why using the section "What I have tried" if you did not try anything? You did not explain any problems, nothing. If you need area and perimeter, calculate them, that's it...
—SA
Patrice T 15-Mar-16 2:36am    
Report your problems to your teacher.
Richard MacCutchan 15-Mar-16 6:28am    
Are you saying that you do not know how to perform these simple calculations? Or is the issue something else?

1 solution

I should not do your home work and also I don't have much experience in Java but a little boost up could help you I think so,

Declare a class like this
Java
class HomeWork{
	
}


You need two methods , one to calculate area and other to perimeter...
Methods are nothing but functions so declare two functions, like this

Java
class HomeWork{
	void CalculateArea(double side)
	{
		System.out.println("Area:  "+ side*side);
	}
	void CalculatePerimeter(double side)
	{
		System.out.println("Perimeter: "+ 4*side);
	}
}


You can see that the functions are getting side like this, funtion(here it gets side)
since area is nothing but the square of side and perimeter is nothing but the sum of the sides. Here the tow functions are getting the side and doing the operations.

In order to call this function in your main class you need an object( you see that class is just a blue print and an object is called as a real world existing thing)

for "What Have you tried" I've taken the code
Java
public class Calculate {
public static void main(String[ ] args) {

}


Ask the user to enter the side and store it in a variable..

Java
public class Calculate {
public static void main(String[ ] args) {
System.out.println("Enter the side");
	Scanner obj = new Scanner(System.in);
	double s = obj.nextDouble();
}


Now you have to make use of an object to perform the operation

Java
HomeWork object = new HomeWork();
	object.CalculateArea(s);
	object.CalculatePerimeter(s);


so there is your output

Here is the main code for your reference
Java
public class Calculate {
   public static void main(String[] args) {
	System.out.println("Enter the side");
	Scanner obj = new Scanner(System.in);
	double s = obj.nextDouble();
	HomeWork object = new HomeWork();
	object.CalculateArea(s);
	object.CalculatePerimeter(s);
}
}


For your ease kindly use this complete code

Java
import java.util.*;
class HomeWork{
	void CalculateArea(double side)
	{
		System.out.println("Area:  "+ side*side);
	}
	void CalculatePerimeter(double side)
	{
		System.out.println("Perimeter: "+ 4*side);
	}
}

public class Main {
   public static void main(String[] args) {
	System.out.println("Enter the side");
	Scanner obj = new Scanner(System.in);
	double s = obj.nextDouble();
	HomeWork object = new HomeWork();
	object.CalculateArea(s);
	object.CalculatePerimeter(s);
}
}


My present knowledge in java ends here from here do refer some online tutorials or ask your Tutor( They get paid to help you) .

Please feel free to ask doubts.
 
Share this answer
 
Comments
Richard MacCutchan 15-Mar-16 12:19pm    
Please do not do people's homework for them. It really does not help then to learn anything other than laziness.
[no name] 15-Mar-16 12:26pm    
Sir, I feel sorry for that but I did't solve his home work , I just helped him. His 3rd and 4th part didn't solved. Shall I delete my answer before the inquirer sees ?
Thank you for your reply
Richard MacCutchan 15-Mar-16 12:33pm    
Yes you gave him a complete solution. Too late to delete it now but plase don't do it in future. Especially as his "What I have tried" section shows no real effort.
[no name] 15-Mar-16 12:36pm    
Thank you for your kind advice sir. This will not happen here after sorry sir
Patrice T 15-Mar-16 17:13pm    
Too much help. Its code does nothing, it don't even give a wrong result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900