System.out.println(1 + 2 + "3" + 4 + 5); System.out.println(6 / 4 * 4);Ans: The order of precedance is the same for + when it means addition and + when it means concatenation. Remember that
number + number --> number string + string --> string number + string --> string string + number --> string Output for first println statement: 3345 For the second println statement: int / int --> int double / int --> double int / double --> double double / double --> double Therefore, 6 / 4 * 4 == 1 * 4 == 4.
double[ ] s = new double[5]; a[2] = 5.1; a[0] = 3.8; a[4] = a[2]; for(double x : a) { System.out.printf("%4.2f ", x); } System.out.println( )
String s = "dog", t = "dog"; String u = new String("dog"); System.out.println(s.equals(t) + " " + s.equals(u)); System.out.println((s == t) + " " + (s == u)); // Output: true true true false
String[ ] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} // Ans: import java.util.Scanner; public class Main { public static void main(String[] args) { String[ ] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; Scanner s = new Scanner(System.in); System.out.print( "Enter the word for a number from 0 to 9: "); String word = s.nextLine( ); int i; for(i = 0; i < words.length; i++) { if(words[i].equals(word)) { System.out.println(i); } } System.out.println("Unknown number"); } } }
class object public private instance variable local variable getter setter instance method argument parameter constructorAns: Items are identified in the Person and Test1 classes.
private int age;and methods with public accessability, for example,
public int getAge( ) { return age; }and
public void setAge(theAge) { age = theAge; }
public class Person { private String name; private char gender; private int age; }Let IntelliJ write the class constructor, getters, setter for age, and the toString method. Use
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class PersonUnitTest { private Person p; @BeforeEach void setUp() { p = new Person("Alice", 'F', 12); } @Test void test() { assertEquals(12, p.getAge( )); p.setAge(10); assertEquals(10, p.getAge( )); p.haveBirthday( ); assertEquals(11, p.getAge( )); } }