You are on page 1of 6

A real estate office handles 50 apartment units.

When the rent is $600 per month,


all the units are occupied. However, for each $40 increase in rent,one unit
becomes vacant. Each occupied unit requires an average of $27 permonth for
maintenance. How many units should be rented to maximize the profit

import java.util.*;
import java.io.*;

public class ChapterFiveHomework


{
static Scanner console = new Scanner(System.in);

public static void main(String[] args) throws FileNotFoundException


{

int units;
double rent;
double startRent;
double rentInc;
double maintenance;
double totalMaintenance;
double earnings;
double earnings2;

System.out.println("Enter the number of appartment units.");


units = console.nextInt();

System.out.println("Enter the rent ammount when all units are occupied.");


rent = console.nextDouble();

System.out.println("Enter the increase in rent that results in a vacant"


+" unit.");
rentInc = console.nextDouble();

System.out.println("Enter the ammount to maintain a rented unit.");


maintenance = console.nextDouble();

earnings2 = (units-1)*(rent+rentInc)-maintenance*(units-1);
earnings = units*rent-maintenance*units;

do
{
units--;
rent = rent+rentInc;
earnings = (units--)*(rent+rentInc)-(maintenance*(units--));
}
while (earnings > earnings2);

System.out.println("Number of units to rent:" + (units));


System.out.printf("Ammount to charge for rent: %.2f", rent);

}
}
Write a program that prompts the user to input a positive ineteger. It should then
output a message indicating whether the number is prime number. Note: 2 is the
only even number that is prime. An odd integer is prime if it is not divisible by an
odd integer less than or equal to the square root of the number.

import java.util.*;

public class prime {

static Scanner kb = new Scanner(System.in);

public static void main() {

System.out.print("\fEnter positive integer:");

int num = kb.nextInt();

if (num < 1)

System.out.print("Please enter number greater than 1"


+ "Perform the program again");

else if (num == 2)
System.out.print("its a prime yey!");

else if (num % 2 == 0)
System.out.print("its not a prime ");

}
}
Write a program that accepts as input the mass (in grams) -8
and density
and outputs the volume of ,)in grams per cubic centimeters(
the object using
the formula: density = mass / volume. Format your output to
two decimal
.places
arrow_forward
Question
Asked Apr 21, 2020
79 views

Write a program that accepts as input the mass, in grams, and density, in
grams per cubic centimeters, and outputs the volume of the object using the
formula: volume 5 mass / density. Format your output to two decimal
places.

check_circle
Expert Answer
Step 1
 

Since, here no programming language is specified. So, we are providing the


solution in Java.

the below given Java program will obey the following rubrics:

 Importing necessary header file.


 Declaring main class volume.
 In the main method, instantiating the scanner class and declaring the
required variable.
 Displaying the message to the user.
 In the do-while loop, reading input of mass and if the mass is less
than zero then, displaying the message to the user to enter valid mass.
 Similarly, reading input of density from user
 If the value of density is less than 0then, displaying a message to the
user to enter valid density.
 Calculating density by using the given formula, density =
mass/volume.
 Displaying calculated results to the user.

 
Step 2
 Program code:

//importing necessary header file

import java.text.DecimalFormat;

import java.util.Scanner;

//class volume

public class Volume

//main method

       public static void main(String[] args)

       {

             //instantiating scanner class

          Scanner scan = new Scanner(System.in);

           //declaring required variable

          double mass = 0;

          double density = 0;

      //dispalying message to user


          System.out.println("Enter mass (in grams): ");

          //do -while loop

          do {

               //reading input of mass

              mass = scan.nextDouble();

              //if mass is less than zero

              if(mass < 0)

              {

              //displaying message to user to enter valid mass

                  System.out.println("Enter valid mass >= 0");

              }

              //while with condition

          } while(mass < 0);

          //displaying message to user

          System.out.println("Enter density (in grams per cubic centimeters): ");

          do {

            ...
This program reads the user's first name and last name,
* separated by a space. It then prints the user's first and
* last names separately, along with the number of characters
* in each name. It also prints the user's initials. Note that
* this program will crash if the user's input does not contain
* a space.
*/
public class FirstNameLastName {

public static void main(String[] args) {

String input; // The input line entered by the user.


int space; // The location of the space in the
input.
String firstName; // The first name, extracted from the
input.
String lastName; // The last name, extracted from the
input.
System.out.println();
System.out.println("Please enter your first name and last
name, separated by a space.");
System.out.print("? ");
input = TextIO.getln();

space = input.indexOf(' ');


firstName = input.substring(0, space);
lastName = input.substring(space+1);

System.out.println("Your first name is " + firstName + ",


which has "
+ firstName.length() + "
characters.");
System.out.println("Your last name is " + lastName + ",
which has "
+ lastName.length() + "
characters.");
System.out.println("Your initials are " +
firstName.charAt(0) + lastName.charAt(0));

You might also like