Question

In: Computer Science

QUESTION 5 Consider the following Die class: class Die { public:    void setValue(int x);// assign...

QUESTION 5

  1. Consider the following Die class:

    class Die {
    public:
       void setValue(int x);// assign x to num
    int getValue(); // return num
    void roll(); // set num with a random number ranging 1-6
    Die();   // constructor
    private:
    int num;
    };

    Which of the following C++ code segment will correctly find that how many times a Die object will be rolled until the value '6' is obtained.

    Die myDie;
    int dieValue, countTil6 = 0;
    myDie.roll();
    dieValue = myDie.getValue();
    while (dieValue != 6) {
    countTil6++;
    myDie.roll();
    dieValue = myDie.getValue();
    }
    cout << countTil6;

    Die myDie;
    int dieValue, countTil6 = 1;
    while (dieValue != 6) {
    myDie.roll();
    dieValue = myDie.getValue();
    countTil6++;
    }
    cout << countTil6;

    Die myDie;
    int dieValue, countTil6 = 1;
    myDie.roll();
    dieValue = myDie.getValue();
    while (dieValue != 6) {
    countTil6++;
    myDie.roll();
    dieValue = myDie.getValue();
    }
    cout << countTil6;

    Die myDie;
    int dieValue, countTil6 = 1;
    while (dieValue != 6) {
    countTil6++;
    myDie.roll();
    dieValue = myDie.getValue();
    }
    cout << countTil6;

3 points   

QUESTION 6

  1. Consider the following statements.

    struct supplierType {
        string name;
        int supplierID;
    };

    struct paintType {
        supplierType supplier;
        string color;
        string paintID;
    };
    paintType paint;

    Which stataement is correct?

    paint.name="Sherwin-Williams";

    paintType.supplyType.name="Sherwin-Williams";

    paint.supplier="Sherwin-Williams";

    paint.supplier.name="Sherwin-Williams";

Solutions

Expert Solution

following C++ code segment will correctly find that how many times a Die object will be rolled until the value '6' is obtained.

Die myDie;
   int dieValue, countTil6 = 0;
   myDie.roll();
   dieValue = myDie.getValue();
   while (dieValue != 6) {
       countTil6++;
       myDie.roll();
       dieValue = myDie.getValue();
   }
   cout << countTil6;

explaination of code

Die will be rolled first and then we get the value of the die

say we get value as 2,, before while loop value of countTil6 is zero

In while loop we check if dieValue is not equal to zero

if yes then we increament the count of variable countTil6

then again we roll the Die and get the value of the Die.. assume we get value as 6

Again we check in while loop if value is 6,,

This time condition is false as we got value 6 ..

So it comes out of the loop and print value countTil6 , in this case 1,, which is correct

=========================================

Below stataement is correct

paint.supplier.name="Sherwin-Williams";

as supplier is the member of struct variable paint and name is the member of struct variable supplier, this is correct statement to assign variable name of the supplier through struct variable paint.

===========

//why other statements are not correct

paint.name="Sherwin-Williams"; //name is not member of paint

paintType.supplyType.name="Sherwin-Williams"; //supplyType type is name of the user defined data type

paint.supplier="Sherwin-Williams";// supplier is the variable of type struct supplyType which has data members name and supplierID


Related Solutions

1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT