Hello, I need someone who can help me with this study guide about Java language
ID: 3785518 • Letter: H
Question
Hello, I need someone who can help me with this study guide about Java language by explaining, answering, and giving examples:
Be able to:Write java methods to perform some sort of processing like displaying output, performing arithmetic,etc.
Properly declare variables and constants.Given the following method signatures, write the method call statement. Include any variabledefinitions that are required:
a. private static double[] createAra(int parmA)
b. private static boolean hasComma(String that)
Determine what a method signature would be for a given call -
a.System.out.println(myMethod());
CSCD 210
b.String s = “a phrase”; int x = getLength(s);c.double pi = 3.14159;float radius = 42;double area = getArea(radius, pi);
Declare a scanner object for the keyboard and assign its value to kb
Determine the output
String s = "Hello!"; for (int i = 0; i < s.length(); i++) { System.out.println(s.charAt(i));
i++;}
String s = "Hello!"; for (int i = s.length() - 1; i >= 0; i--) { System.out.println(s.charAt(i)); }
System.out.println(s);
Explanation / Answer
write method call statement
a. private static double[] createAra(int parmA)
answer:
int parmA=20;
double[] ans=createAra(parmA);
explanation:
see method definition private static double[] createAra(int parmA)
in which double[] is the type of the data returned by method. the square brackets indicates its an array.
So we need to store that data in variable that's why double[] ans is declared.
look at the parameter of method. It is int parmA which is of integer value.that's why I declared int variable parmA and pass it to method.
b. private static boolean hasComma(String that)
answer:
String str="hi,hello";
boolean flag=hasComma(str);
explanation:
as you can see return type is boolean and parameter is of string type.
Determine what a method signature would be for a given call -
a.System.out.println(myMethod());
answer:
private static String myMethod();
explanation:
String is a return type. Practically its not necessary to have only String return type as
System.out.println can take all data type as parameter so you can use any data type as a return type.
b.String s = “a phrase”; int x = getLength(s);
answer:
private static int getLength(String s);
explanation:
as returned data is storedin x which is integer type, so return type of method is int.
and method call pass s as a parameter which is of String type so I declared String s as a parameter in method definition.
It will not raise error if you use same name in call and method definition.
c.double pi = 3.14159;float radius = 42;double area = getArea(radius, pi);
answer:
private static double getArea(float r,double pi);
explanation:
area is double so return type is double.
Declare a scanner object for the keyboard and assign its value to kb
first you have to import util package to use scanner class.you can do it like
import java.util.*
class HelloWorld
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); //new scanner object
int x = sc.nextInt(); // to read integer number;
String str=sc.next(); //to read string.u can also use it to read double, float etc. data types.
}
}
System.in is an InputStream which is typically connected to keyboard input of console programs. it reads keyboard input.
Determine the output
String s = "Hello!";
for (int i = 0; i < s.length(); i++)
{
System.out.println(s.charAt(i));
i++;
}
Output:
H
l
o
explanation:
I will explain with each iteration :
s.length()=6 so 6 iterations
iteration 1: i=0, 0<6 true,
print character at ith i.e. 0 position. so it will be 'H'. print 'H'.
increment i. now i=1;
before moving to next increment for loop goes to increment which is third element in for loop.i++
so increment i. now i = 2
iteration 2 : i=2; 2<6,
print character at 2nd position . i.e 'l'
i++...i=3 .. again i++....now i=4
iteration 3: i=4, 4<6,
print character at 4th position.i.e. 'o'
i++..now i=5 and i++...now i=6
so in next iteration condition i<s.length() becomes false and loop stops
2.
String s = "Hello!";
for (int i = s.length() - 1; i >= 0; i--)
{
System.out.println(s.charAt(i));
}
System.out.println(s);
output:
!
o
l
l
e
H
Hello!
explanation:
the loop starts at i=6 and iterates onedown every time while printing character at ith position.
at the end it prints whole string
Any more explanations required please do comment
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.