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
/* 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...
[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...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add the given Student to the StudentList. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList. In Assign5Test.java do the following: Create a new StudentList containing 3 students. Print the info of all the Students in the StudentList using...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT