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

  Once getting into student information menu

Once getting into student information menu, you should be able to see a full list of students' information (first name, last name, SSN, DOB, year and major).

  Prepare a program that constructs a binary tree

Write your own implementation of primitive operations and use self-referential classes in your implementation.

  Develop a reservation system program for specific flights.

develop a reservation system program for specific flights. A customer should be able to select from one of four categories of seats on flights from RDU to any of at least three other destinations. Based on the user input of seat type and destination,..

  Develop and test stand alone java applications

Gain practical skills in Graphical User Interface (GUI) programming by implementing an event driven application.

  Create a class named blooddata

Create a class named BloodData that includes fields that hold a blood type (the four blood types are O, A, B, and AB) and an Rh factor

  E-marketing-e-commerce consultant

You are an e-marketing and e-commerce consultant. Mary and Joe Johnson, the owners of Book Bunker, have hired you to help them improve their sales both online and in their store.

  Write java programming to show number of credits for course

Write the java programming to solve following problem. We are given a student's marks in Calculus 1, CS1, and Physics 1 (each mark being between 0 and 100) also number of credits for each course.

  Uml exercise

UML Exercise: Automated Teller Machine (ATM),   1. To allow authorized card holders to make transactions,   Brief Summary of Requirements:

  Technical community blog

Write a blog article for a coding/technical community blog

  Data structures and algorithms

Code analysis and programming.

  Write a method called alldigitsodds that returns

Write a method called AllDigitsOdds that returns whether every digit of a positive integer is odd. Return true if the numbers consist entirely of odd digits (1,3,5,7,9)

  You are a mighty warrior and armed with your trusty bow and

you are a mighty warrior and armed with your trusty bow and 3 arrows you enter the caves in search of the mighty

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