Write a java class (TeaParty.java) which has Static short numeric variables, Static SecureRandom object, Main method

  • برمجة جافا

Quiz 2 - CS140 - 375

You will have a virtual Tea Party with your computer sciences colleges and with random numbers of tea cups  and cake slices , you should rate the party as: bad party , good party , or great party .

 

Write a java class (TeaParty.java) which has the following:

Static short numeric variables to represent the amount of tea and cake and initialize their numeric values to 0.

Static SecureRandom object to be used everywhere in your class.

Main method which calls the following methods:

 

o A method called prepareTea as public static void method, which randomly prepares tea cups as random int (it will randomly generate at least zero teacup and at most 30 teacups), then it updates the amount of tea cups. 

o A method called prepareCake as public static void method, which randomly prepares cake slices as random int (it will randomly generate at least 4 teacups and at most 27 cake slices), then it update the amount of cake slices. 

o A method called printResult as public static void method, which prints your opinion of the party as follows:

If either teacup or cake slice is less than 5, then the print statement is ‘Oh no! Bad party!’, check Example 3

If either teacup or cake slice is at least double of each other and at least one of them is at least 5, then the print statement is ‘Wow! Great party!’, check Example 2

If both teacup and cake slice are at least 5, then the print statement is ‘Good party! Yay!’, check Example 1

الأجوبة

import java.security.SecureRandom;

public class TeaParty{
	
	public static short tea = 0;
	public static short cake = 0;
	public static SecureRandom object = new SecureRandom();
	
	public static void main(String[] args){
		
		prepareTea();
		prepareCake();
		printResult();
	
	} // end main method
	public static void prepareTea(){
		
		tea = (short)object.nextInt(31);
	
	} // end method
	public static void prepareCake(){
		
		cake = (short)(4 + object.nextInt(24));
		
	}// end method
	public static void printResult(){
		
		System.out.println("I have put for you: "+ tea + " teacups and " + cake + " cake slices.");
		if(tea < 5 || cake < 5)
			System.out.println("oh no! Bad party!");
		
		if(tea >= 5 && cake >= 5){
			if(tea >= (cake*2) || cake >= (tea*2))
				System.out.println("Wow! Great party!");
			else
				System.out.println("Good party! Yay!");
		}
	} // end method

} // end class
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...