Question

1. Suppose that the integer variables n and m have been initialized. Check all the correct...

1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment.

String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" );

Question 5 options:

If m is strictly greater than 2n then the value of s2 is "two"

If n is equal to m then the value of s2 is "two"

If m is strictly greater than 2n then the value of s2 is "three"

If m is not zero and is equal to 2n, then the value of s2 is "two"

2. Suppose that our Java program is in a package consisting of the classes Utilities, House, and Main, and the class Main contains the main method of the program. Suppose further that class House has a default constructor.

Check below all the correct statements.

Question 6 options:

If class House defines no constructors, then the main method can use the following statement

House hers = new House(2500.30, 5000.50);

If class House has the public accessor method getArea() that returns double, then it is legal to have the following statements in the main method

House mine = new House();
System.out.printf("House area: %.f2\n", mine.getArea());

If class Utilities has the public static String variable mainOffice then it is legal to have the following statement in some method of the class House

System.out.printf("Main office: %s", Utilities.mainOffice);

If class House has the private instance variable price of type double, then it is legal to have the following statements in the main method

House mine = new House();
int total = (int)mine.price;

3.

A balanced brackets string (BBS) is one of the following:
1. the string {}
2. the string {b}, if b is a BBS
For example, {}, {{}} are valid BBSs, but }{, {}}, {3} are invalid BBSs. The empty string is also an invalid BBS.
Select below the one method that correctly implements recursively whether a given string s is a valid BBS.

Question 10 options:

public static boolean isBBS(String s) {
if (s.equals("{}")) return true;
int l = s.length();
if (l<2) return false;
if (s.charAt(0)!='{' || s.charAt(l-1)!='}')
return false;
return true;
}

public static boolean isBBS(String s) {
int l = s.length();
if (l<2) return false;
if (s.equals("{}")) return true;
if (s.charAt(0)=='{' && s.charAt(l-1)=='}')
return isBBS(s.substring(1, l-1));
return false;
}

public static boolean isBBS(String s) {
if (s.equals("")) return false;  
int l = s.length();
if (s.charAt(0)=='{' && s.charAt(l-1)=='}')
return isBBS(s.substring(1, l-1));
return false;
}

public static boolean isBBS(String s) {
int l = s.length();
if (l<2) return false;
if (s.charAt(0)=='{' && s.charAt(l-1)=='}')
return isBBS(s.substring(1, l-1));
return false;
}

Homework Answers

Answer #1

1)

The correct answers are:

  • If m is strictly greater than 2n then the value of s2 is "three"
  • If m is not zero and is equal to 2n, then the value of s2 is "two"

2)

The correct answers are:

  • If class Utilities has the public static String variable mainOffice then it is legal to have the following statement in some method of the class House

    System.out.printf("Main office: %s", Utilities.mainOffice);

3)

The correct code is:

public static boolean isBBS(String s) {
int l = s.length();
if (l<2) return false;
if (s.equals("{}")) return true;
if (s.charAt(0)=='{' && s.charAt(l-1)=='}')
return isBBS(s.substring(1, l-1));
return false;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) ==...
Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1))); } This method will return true if and only if: 1. s.charAt(0) == s.charAt(1) 2. s starts with two or more of the same characters 3. s ends with two or more of the same characters 4. s contains two or more of the same characters 5. s contains two or more of the same character in a row
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private int houseNumber;    private int bedrooms;    private int sqFeet;    private int year;    private int cost;    public House(int houseNumber,int bedrooms,int sqFeet, int year, int cost)    {        this.houseNumber = houseNumber;        this.bedrooms = bedrooms;        this.sqFeet = sqFeet;        this.year = year;        this.cost = cost;    }    public int getHouseNumber()    {        return houseNumber;    }   ...
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
I have a recursive Tower of Hanoi program but don't know how to include the count...
I have a recursive Tower of Hanoi program but don't know how to include the count variable. Could you tell me how? Main.java import java.io.*; import java.util.List; public class Main { //int count = 0; /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk.*/...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT