ASSIGNMENT 3 1. Write a class named QuestionA including the following methods: (
ID: 3709196 • Letter: A
Question
ASSIGNMENT 3
1. Write a class named QuestionA including the following methods: (60 points)
two global variables (named gvar1 and gvar2), both of which are instance variables and integers.
one static variable (named PI), the data type of which is double and the value of which is 3.14159.
an instance method named funA which has two parameters and a return statement. The data type of each parameters is integer. The method returns the sum of two parameters in its return statement.
an instance method named funB which has two parameters and a return statement. The data type of each parameters is double. The method will return the sum of two parameters in its return statement.
an instance method named funC which has one parameter named psum and has no return statement. The data type of the parameter is integer. The method calculates the sum of two gvar1 and gvar2 and then assigns the sum to the parameter psum. Finally, the method prints the value of psum.
a static function named funD which has one parameter named radius and a return statement. The data type of the parameters is integer. The function returns the area of a circle based on the formula: PI*radius*radius.
a main method that will call all methods/functions above. When calling funA, it will use pass-by-value. When calling funB, it will use pass-by-reference. When calling funC, it will use pass-by-out-parameter. When calling funD, it will use pass-by-value.
2. Write a class named QuestionB that only has a main method. In the main method, it will call all methods/functions in the class QuestionA. (40 points)
Explanation / Answer
Java code for Question 1:
//creating class QuestionA
public class QuestionA
{
//two global variables (named gvar1 and gvar2)
int gvar1,gvar2; //are instance variables and integers
//one static variable (named PI)
static double PI=3.14159;
//an instance method named funA
public int funA(int a,int b)
{
// returns the sum of two parameters
return (a+b);
}
//an instance method named funB
public double funB(double a,double b)
{
// returns the sum of two parameters
return (a+b);
}
// an instance method named funC
public void funC(int psum )
{
/*calculates the sum of two gvar1 and gvar2 and
then assigns the sum to the parameter psum.*/
psum=gvar1+gvar2;
// method prints the value of psum.
System.out.println("psum value="+psum);
}
//a static function named funD
static public double funD(int radius)
{
//calculates the area of circle based on the formula
double area=PI*radius*radius;
//returns the area of the circle
return area;
}
//a main method that will call all methods/functions above.
public static void main(String[]arg)
{
//creating object of QuestionA class
QuestionA questionA = new QuestionA();
//calling method funA()
int funAValue=questionA.funA(10, 34);
System.out.println("funA() Result="+funAValue);
//calling method funB()
double funBValue=questionA.funB(1.45,4.99);
System.out.println("funB() Result="+funBValue);
questionA.gvar1=250;
questionA.gvar2=340;
//calling method funC()
questionA.funC(11);
double area=QuestionA.funD(15);
System.out.println("Circle Area="+area);
}
}
Output:
funA() Result=44
funB() Result=6.44
psum value=590
Circle Area=706.85775
Java code for Question 2:
public class QuestionB
{
//only one main method
public static void main(String[]args)
{
//all all methods/functions in the class QuestionA.
//creating object of QuestionA class
QuestionA questionA = new QuestionA();
//calling method funA()
int funAValue=questionA.funA(10, 34);
System.out.println("funA() Result="+funAValue);
//calling method funB()
double funBValue=questionA.funB(1.45,4.99);
System.out.println("funB() Result="+funBValue);
questionA.gvar1=250;
questionA.gvar2=340;
//calling method funC()
questionA.funC(11);
double area=QuestionA.funD(15);
System.out.println("Circle Area="+area);
}
}
Output:
funA() Result=44
funB() Result=6.44
psum value=590
Circle Area=706.85775