In: Computer Science
Explain how to use a Scanner object to read
information from a page on the internet. Give an example.
Explain what a wrapper classes are and how they are used in
Java. Give an example.
What is an ArrayList object? How does is it
similar to and how does it differ from a traditional
array.
What is inheritance? How is it useful in software projects. How
is the is-a test related to inheritance?
What is an exception? Explain how to use try/catch blocks to
prevent a program from crashing.
What is refactoring? What are some of the ways that Eclipse can
help you with refactoring?
What is the output? Explain how you obtain this answer by hand, not using a computer.
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i = 1; i <= 26; i *= 2) {
System.out.print(alphabet.charAt(i - 1));
}
What is the output? Explain how you obtain this answer by hand, not using a computer.
int n = 500;
int count = 0;
while (n > 1) {
if (n % 2 == 0) {
n /= 3;
}
else {
n /= 2;
}
count++;
}
System.out.println(count);
A Java Swing application contains this paintComponent method for a panel. Sketch what is drawn by this method.
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(100, 100, 200, 100);
g.fillRect(300, 200, 200, 100);
g.fillRect(500, 100, 200, 100);
}
(a) scanner input = new scanner (system.in)
By above, we use a scanner object to read information from a page on the internet.
Example:
import java.util.Scanner;
public class Demo{
public static void main(String[] args)
{
int num1, num2, mul;
Scanner input = new Scanner(System.in);
System.out.println("Enter First multiple");
num1 = input.nextInt();
System.out.println("Enter second multiple");
num2 = input.nextInt();
mul = number1 * number2;
System.out.printf("The product of both number is %d" , mul);
}
}
(b) Wrapper Class: To convert primitive into object into primitive is called wrapper class.
| Primitive Type | Wrapper Class |
| boolean | Boolean |
| char | Char |
| byte | Byte |
| short | Short |
| int | Int |
| long | Long |
| float | Float |
| double | Double |
Example:
public class WrapperDemo{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int
int j=a;//unboxing, now compiler will write a.intValue() internally
system.out.println(a+" "+i+" "+j);
}}
(c) Array List Object: Arraylist is a class which implements List interface. ArrayList is a variable length in Collection class. Array list is growable in nature that is decrease and increase its size.
Example:
ArrayList<Integer> integerList = new
ArrayList<Integer>();
integerList.add(1);
Similarity Between Array List and Array:
Difference Between Array List and Array:
(d) Inheritance: It is a OOP's Concept. It allows child class to inherit the property or method of it's parent class.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
It is useful in software projects: It is used for code reusability and to achieve method overriding.
Example:
class Employee{
float salary=4000;
}
class Guard extends Employee{
int bonus=10000;
public static void main(String args[]){
Guard g=new Guard();
System.out.println("Guard salary is:"+g.salary);
System.out.println("Bonus of Guard Salary is:"+g.bonus);
}
}
The is-a test related to inheritance: In above example, Guard is the subclass and Employee is the superclass. Relationship between two classes is Guard IS-A Employee.It means that Guard is a type of Employee. So, this is the is-a test related to inheritance.