BPJ Quiz 15+18 A
-
Make an array of integers whose length is a number $n$ that the user types. Fill it with the perfect squares from $n^2$ down to $1^2$, in that order.
Example:
Number? 10 [Array contains: {100,81,64,49,36,25,16,9,4,1}]
Starter code:
public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.println("Number?"); int n = kb.nextInt(); // Your code here }
-
(
Pencil
) The Pencil class has aboolean mechanical
field and aboolean sharp
field. When you make a new pencil, it is always sharp. A pencil has avoid write()
method which makes non-mechanical pencils get dull. Avoid sharpen()
method makes the pencil sharp. See below for printing out.Pencil a = new Pencil(true); // mechanical pencil Pencil b = new Pencil(false); // classic pencil System.out.println(a); // "Mechanical pencil" System.out.println(b) // "Sharp classic pencil" b.write(); System.out.println(b) // "Dull classic pencil"
-
(
plus19
) Find the sum of all of the elements in the array, except ignore the numbers between the appearance of a 1 and the appearance of the following 9 (inclusive). Every 1 will be followed by at least one 9.Example:
plus19({10,20,30,1,40,50,60,9,70,9}) = 10+20+30+70+9
public static double plus19 (double [] xs)