Question

Make a public class called ManyExamples and in the class... Write a function named isEven which...

Make a public class called ManyExamples and in the class...

Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd.

Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number.

Write a method called sumTo that takes two int parameters and returns an int. This function should return the sum of all numbers from the smaller parameter to the larger parameter, inclusive. (i.e. given 2 and 4, returns 9. Because 2 + 3 + 4 = 9) (if given 4 and 2, returns 9 as well. Because 2 + 3 + 4 = 9)

Write a method named rollSnakeEyes which takes no parameters and does not return any value. This method should continually roll two dice and only terminate when both dice are equal to 1. You must roll both dice at the same time. For example, if one die is 1 you can not only roll the other die. For example, if one die is 1 you can not only roll the other die. Print the result of each roll to the console. (So if die one was a 4 and die two was a 6, print "4   6" to the console). Use '(int)(Math.random() * 6) + 1' to simulate a dice roll. (That code will always produce an int between 1 and 6)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package manyexamples;

import java.util.Scanner;

/**
*
* @author miracle
*/
public class ManyExamples {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Even or not");
Scanner sc=new Scanner(System.in);
System.out.println("Enter integer");
int n=sc.nextInt();
int flag=evenOrNot(n);
if(flag==1){
System.out.println(n+" is even");
}
else
System.out.println(n+" is odd");
  
System.out.println("Nearest to 10");
System.out.println("Enter two integers");
int n1=sc.nextInt();
int n2=sc.nextInt();
int near=nearest(n1,n2);
if(near==0){
System.out.println("tie");
}
System.out.println("Nearest to 10 is "+near);
  
System.out.println("Sum from small parameter to largest parameter");
System.out.println("Enter small integer");
int small=sc.nextInt();
System.out.println("Enter larger integer");
int large=sc.nextInt();
int sum=sumTo(small,large);
System.out.println("Sum from small parameter to largest parameter is:"+sum);
System.out.println("Roll Snake eyes");
rollSnakeEyes();
}
  
  
private static int evenOrNot(int n) {
if(n%2==0){
return 1;}
else return 0;
}

private static int nearest(int n1, int n2) {
  
int near1=Math.abs(10-n1);
int near2=Math.abs(10-n2);
if(near1==near2){
return 0;}
if(near1>near2){
return n2;}
else return n1;


}

private static int sumTo(int small, int large) {
int sum=0;
while(small<=large){
sum=sum+small;
small=small+1;
}
return sum;
}

private static void rollSnakeEyes() {
int die1,die2;
do{
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
System.out.println("die 1:"+die1+" die 2:"+die2);
}while(die1+die2!=2);
  
}

}

Screenshot:

Add a comment
Know the answer?
Add Answer to:
Make a public class called ManyExamples and in the class... Write a function named isEven which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length,...

    WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and...

    help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and an int parameter. If both string parameters represent binary numbers and the int parameter is equal to a positive number less than or equal to the length of the longest string parameter, the function should return a binary string whose length is equal to two times the length of the maximum length of the two string parameters whose value is equal to the sum...

  • Use Python: Dice Rolls Create a function named build_roll_permutations which returns all 36 permutations of rolling...

    Use Python: Dice Rolls Create a function named build_roll_permutations which returns all 36 permutations of rolling two six sided dice. That is, each dice has a number 1 through 6 on one of its sides. The return value is a list which contains tuples. Each tuple represents a possible dice roll of two dice. Card Deck Create a function named build_deck that returns a full deck of cards. The cards are created by a rank and a suit (e.g. 2♡)....

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • Create a class named HW that has the main method. Leave the main method empty for...

    Create a class named HW that has the main method. Leave the main method empty for now. • Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2,...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT