part - a1 to compile and run a java source


PART - A

1.  To compile and run a Java source file "Hello.java" that contains a main() method which of the following are the correct commands?

A.  to compile use "javac Hello.java" then to execute use "java Hello"
B.  to compile use "javac Hello" then to execute use "java Hello.class"
C.  to compile use "javac Hello.java" then to execute use "java hello"
D.  to compile use "javac Hello.java" then to execute use "java Hello.class"  
 
2.  Which of the following Java code fragments compile correctly?

A.  String x = 2;         
B.  String s = "Hello";    
C.  String myName;    myname = "Peter";  
D.  String s;    S = "George";
 
3.  A Java virtual machine runs

A.  The commands in the .java files
B.  The bytecode in the .class files
C.  Does not run any code
D.  It compiles the commands written in the .java files
 
4.  Constructor methods must be defined to have what return type?

A.  int
B.  void
C.  the return type should be the same as the class
D.  constructors do not have return types

5.  Assuming that the following code fragment is part of a class, what will the concatenate method return if passed the string "Mary"?

String s = "Peter";
String concatentate(String s)
{
return s + this.s + s;
}
 
A.  Mary Mary Mary
B.  Peter Peter Peter
C.  Mary Peter Mary
D.  Peter Mary Peter

6.  What is the most likely method signature of the "parseInt()" if it can be called as follows: int c = Integer.parseInt("34");

A.  public void parseInt(String number)
B.  public static void parseInt(String number)
C.  public int parseInt(String number)
D.  public static int parseInt(String number)

7.  In Java, which of the following is the correct way to create a new object of the class MP3?

A.  MP3 downloadSong = new MP3;  
B.  MP3 *downloadSong = new MP3();
C.  MP3 downloadSong = new MP3();
D.  None of the above

8.  What would be the value of variable c after executing the following statement: int c = Integer.parseInt("a");

A.  a
B.  97 which is ASCII value of a  
C.  The statement would throw a NumberFormatException
D.  0
E.  "a"

PART B:
B1.
 
(a) Consider the code below:     
public class ArgsExample {
  public static void main(String[] args) {
    String from = "";
    String to = "";
    String subject = "";
    String emailText = "";
    for (int i = 0; i < args.length; i++) {
      if (args[i].startsWith("From:")) {
        from = args[i];
        System.out.println("MAIL FROM: " + from);
      } else if(args[i].startsWith("To:")) {
        to = args[i];
        System.out.println("RCPT TO: " + to);
      } else if(args[i].startsWith("Subject:")) {
        subject = args[i];
        System.out.println("SUBJECT: " + subject);
      } else if (args[i].startsWith("Body:")) { //The rest
        for (int j = i; j < args.length; j++)
        {
          emailText += args[j];
        }
      System.out.println(emailText);
      System.out.println(".");
      }
    }// for
  }//main
}

Understand the code and observer  the output after running this program with command-line arguments as follows

    > java ArgsExample To:[email protected] From:[email protected] Subject:Hello Body:This is message
 
Look at the code above and run it with above command-line arguments, answer the following two questions:

i.  What is stored in args?

ii.  What is stored in the variable emailText just after the statement

(emailText += args[j];// where j=4) is executed?

(b)

Modify the program above to make the output looks more clear by removing "To:", "From:", "Subject:",   "Body:" and add space for "Thisismessage" (i.e. make it become "This is message"). Also, let MAIL FROM come before RCPT TO. Run your program using the command above. Note that To:[email protected] must be the first command-line argument. The program should output to the screen as follows:

  number of args is: ###
MAIL FROM: [email protected]
  RCPT TO: [email protected]
  Subject: Hello
  This is message

Where ### represents actual number of commend-line arguments. Remember to output the correct value.

We will use the command above in a command window to run your program. We will also use different email addresses, subject, email message, to run your program.

Your program should output to the screen as follows:
 
  number of args is: ###
MAIL FROM: [email protected]
  RCPT TO: xyz@gmail
  Subject: Meeting
  See you at 1pm

It is important that you run your program with the commands above, and check its execution and output before submitting it to ESP.

B2.

Random number generators are frequently used in many applications. In Java you can use method random() from library Math, for example:

double randNum = Math.random();

This statement will return a random number between 0.0 and 1.0 (excluding 1.0). If you need an integer number you have to convert rundNum into int,    for example if you want to generate an integer number 1, 2, ..., 6 you may do in that way:

int i = 1 + (int) ( Math.random() * 6);
 
Write a program that simulate n rolls of six-sided die and displays the frequency of occurrence of each side. For example, If n = 6000, then each side is expected to occur about 1000 times. A sample output could be as follows:

Face 1 frequency= 999
Face 2 frequency= 1031
Face 3 frequency= 1010
Face 4 frequency= 1000
Face 5 frequency= 1026
Face 6 frequency= 934
 
You are required to use the following command line to execute your program in DOS (Windows Console, i.e. cmd.exe).

-  java RollDie6 6000
 
If you use any IDE to create this program, it is important that you run it with our command in DOS, and check its execution and output before submitting it to ESP. We will use different numbers for rolls to test your program, e.g.

-  java RollDie6 12000
 
For example, an output format could be as follows:

-  java RollDie6 6000

Face 1 frequency= 1037
Face 2 frequency= 990
Face 3 frequency= 973
Face 4 frequency= 968
Face 5 frequency= 988
Face 6 frequency= 1044
 
Hints: you can use a for loop to simulate n (e.g. n=6000) rolls of six-sided die, i.e.

for ( int roll = 1; roll <= n; roll++ )

{ /*each time you get an integer number between 1 and 6 generated randomly. e.g. if this number is 3, the frequency of occurrence of the side Face 3 is increased by 1. */ .............

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: part - a1 to compile and run a java source
Reference No:- TGS0486017

Expected delivery within 24 Hours