Question

In: Computer Science

Explain how to use a Scanner object to read information from a page on the internet....

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);
}

Solutions

Expert Solution

(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:

  1. Duplicates are allowed in both. So that, We can insert two or more values.
  2. Insertion order is preserved.

Difference Between Array List and Array:

  1. Array is of fixed size while array list is of variable in length.
  2. Heterogeneous data types are allowed in array list while homogeneous data types allowed in array.
  3. There is no underlying data structure in array while Array list have underlying data structure.

(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.  


Related Solutions

Java How to read a comma separated value file using Scanner for example Scanner sc =...
Java How to read a comma separated value file using Scanner for example Scanner sc = new Scanner(filename); I need to assign each value separated by a comma to different variable types. I need a good example to know how it works and implement it in my project Please only use Scanner to read the file
Use the internet and research the impact that information technology and the internet has on society,...
Use the internet and research the impact that information technology and the internet has on society, the economy, and the environment. Give positive and negative examples. In addition, discuss strategies for safeguarding computers, mobile devices, and networks against attacks while using the internet.
1. Copying and pasting from the Internet can be done without citing the Internet page, because...
1. Copying and pasting from the Internet can be done without citing the Internet page, because everything on the Internet is common knowledge and can be used without citation. True False 2. You don’t have to use quotation marks when you quote an author as long as you cite the author’s name at the end of the paragraph. True False 3. When you summarize a block of text from another work, citing the source at the end of your paper...
Discuss ways an internet site can collect and use information from its visitors.
Discuss ways an internet site can collect and use information from its visitors. You may refer to the site of a hotel, restaurant, club, or a destination marketing organization when answering your question.
Internet Marketing: Whats internet Marketing? Discuss an existing company that use internet marketing? how it use...
Internet Marketing: Whats internet Marketing? Discuss an existing company that use internet marketing? how it use Internet marketing to promote one of its products? which product and why? what are the benefit of internet marketing ? Support Information: As in any new industry or concept, the Internet has its share of confusing buzzwords and jargon. A website produced by Matisse Enzer, presents a comprehensive glossary of Internet terms. http://www.matisse.net/files/glossary.html Many traditional elements of marketing easily translate into Internet marketing such...
Explain how you will use information from sections in Chapter 2 of a dissertation (Background to...
Explain how you will use information from sections in Chapter 2 of a dissertation (Background to the Problem, Theoretical Foundations, and topics in the Review of the Literature) to develop a first draft of Chapter 3 including the Problem Statement, Research Questions, Research Design, Data Collection sources, and Data Analysis. How will you self-score each section using the criteria to help improve the quality of your work and speed up the process of writing your proposal?
Q1. Explain how (EinScan-SE?) 3D Scanner work ?
Q1. Explain how (EinScan-SE?) 3D Scanner work ?
The internet is host to a wealth of information and much of that information comes from...
The internet is host to a wealth of information and much of that information comes from raw data that have been collected or observed. Many websites summarize such data using graphical methods discussed in this chapter. Find a website related to your major that summarizes data and uses graphs, and share it with the class. Let us know how it relates to your major and why it is of interest to you. My major is Accounting
Explain the aim of using the ? table. How can we use and read the ?...
Explain the aim of using the ? table. How can we use and read the ? table ? Draw the graphical part of the table and explain generally.
In a Servlet, how do you read HTML Form Data (data from the previous Web page)...
In a Servlet, how do you read HTML Form Data (data from the previous Web page) into the Servlet?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT