Part - a1nbsp to compile and run a java source file

Assignment Help JAVA Programming
Reference no: EM13371965

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. */ .............

Reference no: EM13371965

Questions Cloud

Read the article narayanan s desiraju r amp chintagunta p k : read the article narayanan s. desiraju r. amp chintagunta p. k. 2004 return on investment implications for
1what is the difference between moral good and moral evil : 1.what is the difference between moral good and moral evil? what is the norm of morality?2.recall the five steps in the
Problem 1 facility location a big retail chain needs to : problem 1 facility location a big retail chain needs to find the best location to build new warehouse. the new
Through the week ending september 162011 tiger woods was : through the week ending september 162011 tiger woods was the leading money winner on the pga tour with total earnings
Part - a1nbsp to compile and run a java source file : part - a1.nbsp to compile and run a java source file hello.java that contains a main method which of the following are
Part 1 - choice under uncertaintylet us assume that your : part 1 - choice under uncertaintylet us assume that your utility function is given by u nbspradici . you have been
1 a university has been tracking the percentage of alumni : 1. a university has been tracking the percentage of alumni giving to its annual fund each for the past 10 years. the
1post-war recessions in the ukusing the data given in the : 1.post-war recessions in the ukusing the data given in the file1.a identify the periods of recession in the united
Multiply by using shift and add instructionsyou have : multiply by using shift and add instructionsyou have studied shift and rotate instructions. when a number is shifted

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd