P.S. I\'m not using any software to compile and run and I dont wish to. I am usi
ID: 3633318 • Letter: P
Question
P.S. I'm not using any software to compile and run and I dont wish to. I am using the traditional method (Notepad, command prompt, etc.).
The code is as follows:
import java.util.*
;public class Olympics
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int d1,d2,tot; //This is die1, die2, and total
do
{
d1=(int)(Math.random()*6)+1; //used math method provided by SUN
d2=(int)(Math.random()*6)+1; //used math method provided by SUN
tot=d1+d2;
System.out.println("die1: "+d1+" die2: "+d2+" total: "+tot); //this will show the dices being thrown until 7 or 11 appears
}
while(tot!=7&&tot!=11); // The 1st event stops here as a 7 or 11 appeared
String s1,s2; // beginning of second event
System.out.print("enter a string: "); // Here I ask the user to input string #1
s1=in.next();
System.out.print("enter another string: "); // Here I ask the user to input string #2
s2=in.next();
System.out.println("you strings in alphabetical order"); // The two strings will appear here, And will be displayed in alphabetical order
if(s1.compareToIgnoreCase(s2)<0)
System.out.println(s1+" "+s2); //string 1 output
else
System.out.println(s2+" "+s1); // string 2 output
System.out.println(s1+" has "+s1.length()+" characters"); //here it shows the number of characters for the first string
System.out.println(s2+" has "+s2.length()+" characters"); // here it shows the number of characters for the second string
System.out.println(s1+" as all upper case: "+s1.toUpperCase()); //the first string, shows all letters in uppercase
System.out.println(s2+" as all lower case: "+s2.toLowerCase()); //the second string, show all letters in lower case
// end of Second event
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class MathFunctions
{
public static void main(String[] args)
{
System.out.println("Math Functions : ");
MathFunction();
System.out.println("String Functions : ");
try {
StringFunction();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void MathFunction()
{
Random rand = new Random();
int dice1 = 0, dice2 = 0, sum = 0;
int min = 1, max = 6;
while(true)
{
rand = new Random();
dice1 = min + rand.nextInt(max-min);
dice2 = min + rand.nextInt(max-min);
sum = dice1+dice2;
System.out.println("Dice 1 = "+dice1+", Dice 2 = "+dice2+" , Total(dice1+dice2) = "+sum);
if(sum !=7 && sum != 11)
{
System.out.println("Tossing the dice again.....");
}
else
break;
}
}
public static void StringFunction() throws IOException
{
InputStreamReader inp=new InputStreamReader(System.in);
BufferedReader input =new BufferedReader(inp);
String str1,str2;
System.out.println("Enter string 1 >>");
str1 = input.readLine();
System.out.println("Enter string 2 >>");
str2 = input.readLine();
if(str1.compareTo(str2) > 0)
{
System.out.println(str2 +" "+str1);
}
else
{
System.out.println(str1 +" "+str2);
}
System.out.println("Number of characters in string 1 : "+str1.length());
System.out.println("Number of characters in string 2 : "+str2.length());
System.out.println(str1.toUpperCase());
System.out.println(str2.toLowerCase());
}
}
it's not allowing me to submit with empty answer. so i am posting the same answer again.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.