Which of the following statements correctly declares and initializes a String object named greeting with the value "Hello" in Java?
AP (Advanced Placement) · AP Computer Science A
Introduction to Algorithms, Programming, and Compilers:練習題
5 條多項選擇題即時批改,另有 1 條文字題附完整解題步驟,全部圍繞「Introduction to Algorithms, Programming, and Compilers」。
Consider the following code segment:
Integer num1 = new Integer(10);
Integer num2 = 20;
int sum = num1 + num2;
Which of the following concepts are demonstrated in this code?
Suppose a class Rectangle has a constructor public Rectangle(int w, int h) and a method public void scale(int factor). What happens in memory when the following is executed?
Rectangle r1 = new Rectangle(10, 20);
Rectangle r2 = r1;
r1 = new Rectangle(5, 5);
r2.scale(2);
Given the following code snippet:
String s = "Computer Science";
What is the result of the method call s.substring(3, 7)?
Consider the following method call to the Math class:
double result = Math.pow(Math.sqrt(16), 2);
What is the value of result?
You are using the String and Math classes to process geometric data provided as text.
Part a: Given a string coord = "(x1, y1)", write the Java code to extract the numerical values of \(x_1\) and \(y_1\) using indexOf and substring methods, assuming they are integers.
Part b: Use the Math.pow and Math.sqrt methods to calculate the distance \(d\) from the origin \((0,0)\) to the point \((x_1, y_1)\) using the formula \(d = \sqrt{x_1^2 + y_1^2}\).
Part c: If the coordinates are retrieved from a 2D grid where \(x_1 \in [0, 100]\) and \(y_1 \in [0, 100]\), use Math.random() to generate a random point and calculate its distance from the origin. Explain how you ensure the random value is an integer.
先自己寫一次答案,再對照解題步驟。
* thinka提供的內容由AI生成,可能並非總是準確或最新。請將其用作輔助資源,並與官方材料進行核實。
想多做幾條同類題目?立即開始練習呢個課題,即做即批改。
立即練習