Question

IN JAVA 1. Write up a small program that accepts two integers from the user. Print...

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.

Homework Answers

Answer #1

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...


Know the answer?
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
Similar Questions
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the range [0-100] as input from the user. Your program should then determine and print the smallest and largest integers in the values entered by the user using comparison operators *not* using predefined min or max Python functions. For instance: If the user input is: 36 5 99 your program should give as output: min = 5 max = 99 If the user input is:...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
x86 irvine library assembly code Write a complete program that: 1. Prompt the user to enter...
x86 irvine library assembly code Write a complete program that: 1. Prompt the user to enter 10 numbers. 2. save those numbers in a 32-bit integer array. 3. Print the array with the same order it was entered. 3. Calculate the sum of the numbers and display it. 4. Calculate the mean of the array and display it. 5. Rotate the members in the array forward one position for 9 times. so the last rotation will display the array in...