myClass class 6ab int main [ab] %a ab+7 Class (7)
class
: reserved word.
6ab
: starts with a digit.
int
: reserved word.
[ab]
: contains "[" and "]"
%a
: contains "%"
ab+7
: contains "+"
(7)
: contains "(" and ")"
Question b) What is the output of the following program? Write a method that returns the BMI of a person. It takes the weight and height in kg and m, respectively.
Assume that the actual parameters have valid values.
A simple call would be
For example, for the string
public class Q1 {
public static void main (String[] args) {
int a = 3;
int b = a + 4;
b = b + a;
System.out.println(b);
}
}
Answer:
10
aBMI = bmi(81.2, 1.73);
Answer:
/**
* Computes the BMI of a person
* @param weight the weight of the person, in kg
* @param height the height of the person, in m
* @return the BMI
*/
public static double bmi(double weight, double height) {
return weight/height/height;
} // bmi
countDigits
, that takes a string argument, and returns the number of digits in the argument. Write its javadoc comments, as well.
"ab7Ag627"
, it returns 4
.
Answer:
/**
* Counts the number of digits in a string
* @param str the string
* @return the number of digits in a str
*/
public static int countDigits(String str) {
int count = 0;
for (int i=0; i < str.length(); i++)
if (Character.isDigit(str.charAt(i)))
count++;
return count;
} // countDigits
initializeIntArray
, that takes two arguments;
the first arguments is and int[]
and the second is int
.
This method sets the value of each element of the array to the second argument.
Write its javadoc comment, also.
Answer:
/**
* Sets the value of each element of the array to the second argument.
* @param array, array to be initialized
* @param value, to be the value of each element
*/
public static void initializeIntArray(int[] array, int value) {
for (int i=0; i < array.length; i++)
array[i] = value;
} // initializeIntArray
A sample call:
public static void main(String[] args) {
int[] a = new int[10];
initializeIntArray(a, 5);
}
Watch
is defined as below:
public class Watch {
private boolean digital;
private static int hours = 12;
private int minutes;
private String color = "Gray";
public Watch (){
}
public Watch (int hours){
this.hours = hours;
}
public Watch (int hours, int minutes){
this.hours = hours;
this.minutes = minutes;
}
public Watch (String color){
this.color = color;
}
public int getHours () {
return hours;
} // getHours
public void setHours(int hours) {
this.hours = hours;
} // setHours
public int getMinutes () {
return minutes;
} // getMinutes
public void setMinutes(int minutes) {
this.minutes = minutes;
} // setMinutes
public String getColor () {
return color;
} // getColor
public void setColor(String color) {
this.color = color;
} // setColor
} // Watch
What is the output of the following code segment?
public static void main(String[] args) {
Watch myW1 = new Watch(1, 2);
Watch yourW1 = new Watch(3);
Watch herW1 = new Watch("Pink");
System.out.println(myW1.getHours()+" "+myW1.getMinutes());
System.out.println(yourW1.getHours()+" "+yourW1.getMinutes());
System.out.println(herW1.getHours()+" "+herW1.getMinutes());
}
Answer:
3 2
3 0
3 0
Answer:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Q6_1 {
public static void main(String[] args) throws IOException {
File inFile = new File ("original.txt");
Scanner in = new Scanner (inFile);
PrintWriter out = new PrintWriter ("copy.txt");
String line;
while (in.hasNextLine()) {
line = in.nextLine();
out.println(line);
} // while
in.close();
out.close();
} // main
} // class Q6_1
import java.util.Scanner;
public class Quiz7_1 {
public static void main(String[] args) {
String text1 = "123";
Scanner sc1 = new Scanner(text1);
sc1.useDelimiter("");
while (sc1.hasNext()) System.out.println(sc1.next());
String text2 = "12 abr \t 12.75 false \n ABC";
Scanner sc2 = new Scanner(text2);
while (sc2.hasNext())
if(sc2.hasNextDouble())
System.out.println(2 * Double.parseDouble(sc2.next()));
else
System.out.println(sc2.next());
} // main()
} // class
Answer:1
2
3
24.0
abr
25.5
false
ABC
Answer:
public class Sum {
public static void main(String[] args) {
int sum =0;
for (String a: args)
sum += Integer.parseInt(a);
System.out.println(sum);
} // main()
}