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生成,可能并非总是准确或最新。请将其用作辅助资源,并与官方材料进行核实。
想多做几道同类题目?立即开始练习这个课题,边做边批改。
立即练习