Write a program that simulate n rolls of six-sided die

Assignment Help JAVA Programming
Reference no: EM1398005

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

Questions Cloud

Leading money winner on the pga tour : Through the week ending September 16,2011, tiger woods was the leading money winner on the PGA tour with total earnings of $5,517,777. Of the top 10 money winners, 7 players used a title it's brand golf ball. Suppose that we randomly select 2 of t..
Explain current commitment to helping others : Explain current commitment to helping others help themselves also your academic and professional goals once you graduate. How will those goals continue spirit of Dr. Ford's work?
Experiment for chromosomal dna movement : Every cell in the human body has two alleles that condense into single chromosomes held together by a centromere. These "sister" chromaatids replicate and pair with the newly made homologous chromosomes.
State the experimental and null hypotheses : Create an research question, and then state the experimental and null hypotheses. Then, explain why null and alternative hypotheses have to be mutually exclusive. Finally, discuss how Type I and Type II errors are related to your hypotheses.
Write a program that simulate n rolls of six-sided die : Write a program that simulate n rolls of six-sided die and displays the frequency of occurrence of each side and What is the most likely method signature of the "parseInt() - TNE60003
Multilinear regression equation along with p-values : Suppose you went to the Bureau of Labor Statistics web site and found several predictors of your salary. State three possible predictors and a simulated multilinear regression equation along with their p-values. What does your p-values tell you ab..
Explain the strain theory : explain "strain theory," what are the sources of strain, and how do people cope with it? (Please explain having trouble fully understanding strain theory)
Anova for factorial experiments : The data are number of trials to learn to go left at the first turn in a maze for each of their 20 rats. Answer each of the four questions and don't forget to include a verbal statement of your interpretation of the outcomes.
Choice of analysis tool : A random sample of 500 participants was recruited for the study ? Subjects were allowed to elect to participate in an 3-month exercise program or serve as a control subject (190 chose the program, 310 chose to serve as controls)?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write program in java-calculate and display mortgage payment

Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage.

  Write java program to read present basic hourly wage

Write down a java program which will input 1. Read present basic hourly wage. Read in workers fist name and socond name.

  Create a java program to calculate the circumference

Create a Java program based on the geometric shapes. The program should begin by prompting you for the shape you want to calculate the circumference.

  Write function in javascript to compute person-s gross pay

Write the function using JavaScript syntax to compute a person's gross pay for a week. The function should receive the number of hours worked and the rate of pay per hour.

  Recognize one high severity vulnerability for computer

Recognize one high severity vulnerability for each computer (if there is one). Explain vulnerability and describe control(s) to minimize risk from vulnerability.

  Multiple choice java programming questions

Determine which of the following may be a violation of information hiding if inserted for the comment above?

  Write java program which will permit user to make selection

Write the Java Program which will permit the user to make selection. You will present user with two options to perform, then you will perform action selected by user.

  Describe principles of data abstraction and inheritance

Study the principles of data abstraction, inheritance and dynamic binding. Use Library to get started on finding resources.You must take the terms and describe it.

  Java project

Prompt the user for an int between lower and upper boundary.

  Write the bubble sort

The village of Marengo conducted a census and collected records that have household data, including the number of occupants in each household.

  Build the gui layout of the game

Build the Build the GUI layout of the game in java.

  Determine product a inferior or normal good

Research illustrates that prices of related goods are given by Py = $5,900 and ; Pz = $90, while average income of individuals consuming this product is M = $55,000. Is X inferior or normal good?

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