Let's say you've calculated both the length and width of a floorboard and worked out the room area, how would you go about multiplying 10% to the room area to work out the wastage in order to obtain the final total area of the floorboard?
This is what I have done so far:
import java.util.Scanner;
class Floor1 {
public static void floor1(String[] args) {
obtainLength();
obtainWidth();
obtainRoomArea(length, width);
obtainFinalResult();
System.exit(0);
}
public static void obtainLength ()
Scanner in = new Scanner(System.in);
System.out.println("Enter length of room:");
float length = in.nextFloat();
}
public static void obtainWidth ()
Scanner in = new Scanner(System.in);
System.out.println("Enter width of room:");
float width = in.nextFloat();
}
public static void obtainRoomArea (float Length, float Width){
float RoomArea = RoomArea(Length, Width);
System.out.println("Area of room is:" + RoomArea);
}
public static void obtainFinalResult()
final double total = 0.1;
wastage =(float RoomArea* final double total);
System.out.println("The total calculations including wastage is:" + wastage);
}
}
I am only retrieving the area of the room. I'm unable to retrieve the final results. Could you please show me where I'm going wrong with examples please? Many thanks!!
Related
In this HackerRank challenge, I need to find the total meal cost by adding tip_percent which is 20% of the meal_cost and tax_percent which is 8% of the meal_cost and the meal_cost being $12. So the output must be a round number of 15 but my output comes out as $14.
It does seem to work properly with custom values like $12.50 for meal_cost which later totaled comes out as a rounded value of $16. What am I doing wrong here?
static double findMealTotal(double meal_cost, int tip_percent, int tax_percent) {
tip_percent = (int)(meal_cost * tip_percent)/100;
tax_percent = (int)(meal_cost * tax_percent)/100;
return meal_cost + tip_percent + tax_percent;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
double meal_cost = scanner.nextDouble();
int tip_percent = scanner.nextInt();
int tax_percent = scanner.nextInt();
//changed solve to mealTotal
double mealTotal = findMealTotal(meal_cost, tip_percent, tax_percent);
System.out.println(Math.round(mealTotal));
scanner.close();
}
You are using integers. Integers are rounded, so you loose precision over the next calculation. Try using doubles and cast to int at the end.
static void Main(string[] args)
{
double cost = findMealTotal(12, 20, 8);
Console.WriteLine(cost.ToString());
}
static double findMealTotal(double meal_cost, int tip_percent, int tax_percent)
{
double tip = meal_cost * tip_percent / 100;
double tax = meal_cost * tax_percent / 100;
return meal_cost + tip + tax;
}
And don't reuse parameters inside your function. It is bad practice.
I'm a beginner at Java and I'm having trouble understanding why my "Inflate" and "getVolume" methods aren't working. I'm sure they're just simple problems but I'd still like some help so I can fix my code and improve!
import java.util.Scanner;
public class Balloon
{
public static void main(String[] args)
{
Scanner multiplier = new Scanner(System.in);
System.out.println("How much should the radius be increased by? ");
double amount = multiplier.nextDouble();
double radius = 0;
public void Inflate(double amount);
{
double newRadius = radius + amount;
}
public double getVolume();
{
double sVolume = (4/3)*Math.PI*(newRadius*newRadius*newRadius);
System.out.print(sVolume);
}
}
}
I suppose that Ballon is an object you can inflate and has an state of radius, you can moreover get the volume.
The main method here is only to test if balloon works correctly
public class Balloon {
private double radius = 0;
public static void main(String[] args) {
Scanner multiplier = new Scanner(System.in);
System.out.println("How much should the radius be increased by? ");
Balloon balloon=new Balloon();
double amount = multiplier.nextDouble();
balloon.inflate(amount);
double volume = balloon.getVolume();
System.out.print(volume);
}
public void inflate(double amount) {
radius = radius + amount;
}
public double getVolume() {
double sVolume = (4 / 3) * Math.PI * (Math.pow(radius, 3));
return sVolume;
}
}
I'm beginning to learn more about Java and I'm trying to code a Gratuity calculator that takes user Input, and shows how much a tip would be at %10 and %20 of the total. I'm getting a single "Cannot make a static reference to the non-static method" error that I can't resolve.
Gratuity class:
public class Gratuity{
//variables
private double total = 0;
private double grat1 = 0;
private double grat2 = 0;
public Gratuity(float value){
total = value;
}
start getters and setters
public double getTotal() {
return total;
}
//method to do the calculations
public void calcGrat(){
grat1 = total * .10;
grat2 = total * .20;
}
public double getGrat1(){
return grat1;
}
}
And the class with the main method:
import java.util.InputMismatchException;
import java.util.Scanner; //import package to use the scanner input function
//TestGrat main class contains method
public class TestGrat {
Scanner keyboard = new Scanner(System.in);
//method to prompt user for total, double is total
public void askForInput(){
try{
System.out.println("Enter the total amount of your bill");
total = keyboard.nextDouble();
}
catch(InputMismatchException e){
System.err.printf("Error, please try again. Program will now close");
System.exit(0);
}
}
public Scanner getKeyboard() {
return keyboard;
}
public void setKeyboard(Scanner keyboard) {
this.keyboard = keyboard;
}
//main method
public static void main(String[] args){
// asks for input in float form
float value = askForInput();
//Creating the gratCalc object and storing value as a float (total)
Gratuity gratCalc = new Gratuity(value);
// get the total value and set as float
float tot = (float)gratCalc.getTotal();
// converting the float value into string
System.out.println("You have entered: " + Float.toString(tot));
gratCalc.calcGrat(); //sets grat
// Displaying the options to user
System.out.println("Below are the tips for %10 as well as %20 ");
//getting the value and then displaying to user with toString
float getNum = (float) gratCalc.getGrat1();
float getNum1 = (float) gratCalc.getGrat2();
// using the value of getNum as float to put into toString
System.out.println( "For %10: " + Float.toString(getNum));
System.out.println(" For %20: " + Float.toString(getNum1));
}
}
Any help would be appreciated. Thanks!
askForInput() is inside your class TestGrat. However, in main() you are calling it directly, as if it was static. You probably meant:
TestGrat test = new TestGrat();
float value = test.askForInput();
askForInput() is also returning void, so you probably want to fix that too.
I want to create a program in Java, which -- basing on user inputs -- finds the area of the entered shape. But I failed to achieve that.
This is my script:
import java.util.Scanner;
public class area {
static Scanner advance = new Scanner(System.in);
public void main(String[] args) {
nu();
}
int length;
int height;
int area;
public void nu(){
String message = advance.nextLine();
if (message.equalsIgnoreCase("rectangle")){
System.out.println("Enter the length of the rectangle: ");
length = advance.nextInt();
//length declared.//
System.out.println("Enter the height of the rectangle");
height = advance.nextInt();
//Height has been declared.//
area = length * height;
System.out.print("The area is: " + area);
}
}
}
First problem is, that this code is not running and so I don't know if its working. All the other things are fine. Can you tell me, what I'm doing wrong?
You need to add static to the main method and create a new instance of area. See the code below.
public class area {
static Scanner advance = new Scanner(System.in);
public static void main(String[] args) {
new area().nu();
}
int length;
int height;
int area;
public void nu(){
String message = advance.nextLine();
if (message.equalsIgnoreCase("rectangle")){
System.out.println("Enter the length of the rectangle: ");
length = advance.nextInt();
//length declared.//
System.out.println("Enter the height of the rectangle");
height = advance.nextInt();
//Height has been declared.//
area = length * height;
System.out.print("The area is: " + area);
}
}
}
You need to add static to the main method of your program.
You won't be able to directly call the nu() method from the main, as it is instance method to call it you will need to create an object of your class.
public static void main(String[] args) {
area a = new area();
a.nu();
}
The other alternate is that you can make your nu() method to be static but then you will not be able to use int length;int height;int area; instance variables directly in your static method nu(). Then you will either need to make these variables to be static too or create an object of the class and then use these variables as per your need.
Because of Java program can't find the main method to run:
We need to add static keyword in 3 places according to your code.
Please try with below full source code:
import java.util.Scanner;
public class area {
static Scanner advance = new Scanner(System.in);
public static void main(String[] args) { //1st change
nu();
}
static int length; //2nd change
static int height; //2nd change
static int area; //2nd change
public static void nu(){ //3rd change
String message = advance.nextLine();
if (message.equalsIgnoreCase("rectangle")){
System.out.println("Enter the length of the rectangle: ");
length = advance.nextInt();
//length declared.//
System.out.println("Enter the height of the rectangle");
height = advance.nextInt();
//Height has been declared.//
area = length * height;
System.out.print("The area is: " + area);
}
}
}
How do i call the length and width variable into the getArea method without creating a private variable in the class, the way I'm doing it is causing the method to run again after its already ran once. I really don't like it this way but thats the way the professor wants it done to simulate the times before "object oriented programming"
/*This program allows the user to enter the length and widtch and receive the area
of the rectangle*/
import java.util.Scanner;
public class theRectangleCompany
{
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getLength();
getWidth();
getArea();
}
public static double getLength()
{
double length;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the length ");
length = keyboard.nextDouble();
return length;
}
public static double getWidth()
{
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the width ");
width = keyboard.nextDouble();
return width;
}
public static void getArea()
{
double length = getLength();
double width = getWidth();
double area = width * length;
System.out.println("The area of the Rectangle is: " +area);
}
}
Why are you calling getLength() and getWidth() from the main method. Just call getArea()
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea();
}
You could make the getArea function take parameters, and use the function calls to the other two functions as the parameters:
getArea(getLength(), getWidth());
public static void getArea(double length, double width) { ... }
changes are here:
/*This program allows the user to enter the length and widtch and receive the area
of the rectangle*/
import java.util.Scanner;
public class theRectangleCompany
{
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea();
}
public static double getLength()
{
double length;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the length ");
length = keyboard.nextDouble();
return length;
}
public static double getWidth()
{
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the width ");
width = keyboard.nextDouble();
return width;
}
public static void getArea()
{
double length = getLength();
double width = getWidth();
double area = (width * length);
System.out.println("The area of the Rectangle is: " +area);
}
}
Not sure if this is what you want:
public static void getArea()
{
System.out.println("The area of the Rectangle is: " + (getLength() * getWidth()));
}
You'd also need to change the main method to exclude the getLength() and getWidth():
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea();
}
An variant to the above is something like
public static void main(String[] args)
{
System.out.print("This program will find an area of a Rectangle ");
getArea(getLength(),getWidth());
}
public static void getArea(double length, double width)
{
System.out.println("The area of the Rectangle is: " + (length * width));
}