IN JAVA
1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same.
2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values.
3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e., a 3 may be either no bueno or average, up to you.
0-3: No bueno
3-5: Average
5-7: Okay
7-10: Great
4. Write a program to read in a user value as an integer. also read in a string from the user. Print the string as many times as the integer value. That is, if the user enters 3 and the string "dog", print out DogDogDog.
5. Write a class that represents a Pizza. Include two data fields to represent the toppings and cost. Include a constructor to create the pizza. Include a method to print the cost of the pizza.
Question
1:
Write up a small program that accepts two integers from the user.
Print which of the two values is bigger. If they are the same,
print that they are the same.
Code:
import java.util.Scanner;
public class BiggestNumber
{
public static void main(String[] args)
{
Scanner src=new
Scanner(System.in);
//Accepts two integers from the
user.
int a=src.nextInt();
int b=src.nextInt();
//Print which of the two values is
bigger. If they are the same
if(a>b)
System.out.println(a+" is greater than "+b);
else if(a<b)
System.out.println(a+" is less than "+b);
//print that they are the
same.
else
System.out.println("Both the numbers are same");
}
}
Question
2:
Write a method that accepts three doubles.
Calculate and return the average of the three
passed double values.
Code:
public static double AverageOfThree(double a,double b,double
c)
{
return (a+b+c)/3.0;
}
Question
3:
Write up a method that accepts a score between 0-10. Print out a
message according to the following table. You ay personally decide
the boundaries. i.e., a 3 may be either no bueno or average, up to
you.
Code:
public static void PrintBoundaries(int number)
{
if(number>=0 &&
number<=3)
System.out.println("No bueno");
else if(number>3 &&
number<=5)
System.out.println("Average");
else if(number>5 &&
number<=7)
System.out.println("Okay");
else if(number>7 &&
number<=10)
System.out.println("Great");
}
Question 4:
Write a program to read in a user value as an integer. also read in
a string from the user. Print the string as many times as the
integer value. That is, if the user enters 3 and the string "dog",
print out DogDogDog.
Code:
import java.util.Scanner;
public class PrintName
{
public static void main(String[] args)
{
Scanner src=new
Scanner(System.in);
//Write a program to read in a user
value as an integer.
String str=src.next();
//also read in a string from the
user. Print the string as many times as the integer value.
int count=src.nextInt();
//Capitalize the first char
String
Newstr=(str.charAt(0)+"").toUpperCase()+str.substring(1,
str.length());
//print out the string 3 times
repeated
for(int i=0;i<3;i++)
System.out.print(Newstr+"");
}
}
Question
5:
Write a class that represents a Pizza. Include two data fields to
represent the toppings and cost. Include a constructor to create
the pizza. Include a method to print the cost of the pizza.
Code:
//Write a class that represents a Pizza
public class Pizza
{
//nclude two data fields to represent the toppings and
cost.
static String toppings;
static double cost;
//Include a constructor to create the pizza.
Pizza(String toppings,double cost)
{
this.toppings=toppings;
this.cost=cost;
}
//Include a method to print the cost of the
pizza.
public static void PrintCost()
{
System.out.println("The cost of the
pizza is "+cost);
}
}
Please check the compiled
program and its output for your reference:
Output:
Code:
Output:
Code:
Output:
(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...
Get Answers For Free
Most questions answered within 1 hours.