sinif 6ab number of seats a.b _a_ a% float (7)_ main Neyin
6ab
: starts with a digit.
number of seats
: contains the space character
a.b
: contains the "." character
a%
: contains "%"
float
reserved word
(7)_
: contains "(" and ")"
Question b) What is the output of the following program? Write a method that returns true if the person is obese, false otherwise.
public class Q1 {
public static void main (String[] args) {
int a = 3;
int b = a + 4;
b = b * a;
System.out.println(b);
}
}
Answer:
21
Answer:
/**
* Checks if a person is obese
* @param weight the weight of the person, in kg
* @param height the height of the person, in m
* @return true if the person is obese
*/
public static boolean obese(double weight, double height) {
return weight/height/height > 30;
} // obese
Answer:
/**
* Computes the number of upper case characters in its argument string.
* @param str in which the number of upper case characters is to be counted
* @return the number of aupper case characters
*/
public static int countUpperCase(String str) {
int count = 0;
for (int i=0; i < str.length(); i++)
if (Character.isUpperCase(str.charAt(i)))
count++;
return count;
} // countUpperCase
displayDoubleArray
,
that takes a double
array and a size
, as its parameneters,
then displays the first size
many elements of the array, nicely.
If the length of the array is less than or equal to the size
,
your method should display all of the elements of the array.
Answer:
/**
* .Displays the first size elemets of the double array
* @param da, doble array
* @param size
*/
public static void displayDoubleArray (double[] da, int size) {
if (size > da.length) size = da.length; // size = size > da.length ? da.length : size;
System.out.print("{" + (size > 0 ? da[0] : ""));
for (int i=1; i < size; i++)
System.out.print(", "+da[i]);
System.out.println("}");
} // displayIntArray
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
Sample run:
Enter the name of the file to read: original.txt
Enter the name of the file to write: copy.txt
Answer:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Q6_2 {
public static void main(String[] args) throws IOException{
Scanner kb = new Scanner(System.in);
System.out.print("Enter the name of the file to read: ");
String inFileName = kb.next();
File inFile = new File (inFileName);
Scanner in = new Scanner (inFile);
System.out.print("Enter the name of the file to write: ");
String outFileName = kb.next();
PrintWriter out = new PrintWriter (outFileName);
String line;
while (in.hasNextLine()) {
line = in.nextLine();
out.println(line);
} // while
n.close();
out.close();
} // main
} // class Q6_2
import java.util.Scanner;
public class Quiz7_2 {
public static void main(String[] args) {
String text1 = "xyz";
Scanner sc1 = new Scanner(text1);
sc1.useDelimiter("");
while (sc1.hasNext()) System.out.println(sc1.next());
String text2 = "12 abr \t 12.7 false \t 140";
Scanner sc2 = new Scanner(text2);
while (sc2.hasNext())
if(sc2.hasNextByte())
System.out.println(3 + sc2.nextByte());
else if(sc2.hasNextDouble())
System.out.println(2 + Double.parseDouble(sc2.next()));
else
System.out.println(sc2.next());
} // main()
} // class
Answer:
x
y
z
15
abr
14.7
false
142.0
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()
}