Java Explain how to use a Scanner object to read information from a page on the
ID: 3795121 • Letter: J
Question
Java
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?
Explanation / Answer
(a) Scanner object is used to read information from a page on the internet as follows:
(b) Wrapper Class: Wrapper Class is used to convert primitive data types into object and object into primitive data type. There are 8 types of wrapper class in java :
Boolean, Character, Byte, Short, Integer, Long, Float, Double.
Wrapper classes are used to convert primitive data types into object and object into primitive data type.
Example:
public class WrapperPrimToWrap{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
(c) Array List Object: Arraylist is a class which implements List interface. It is a child class of list in collection in java. It is growable in nature that is decrease and increase it's size.
Similarity between Traditional Array and Array List:
Difference between Traditional Array and Array List:
(d) Inheritance: It is a Concept of OOP's. It provides code reusability and also used to achieve overriding. In this child class can inherit the properties and methods of it's parent class by 'extends' keyword. It is also known as IS-A relationship.
It provides code reusability and also used to achieve overriding.So, It is useful in software projects.
Example:
class Employee{
float salary=40000;
}
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 is:"+g.bonus);
}
}
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, It is the is-a test related to inheritance.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.