-
Homepage
-
Java Fundamentals Homework Help
A Program that uses PrintIn
Write a
java program that uses println to print on separate lines, your first name, your favorite hobby, your favorite car, your favorite book, and your favorite movie. Be sure to label each line so that the information is understandable. You must simply type the information for the 5 fields in the program itself.
Code
public class Program1 {
public static void main(String[] args) {
String name = "John Smith";
String hobby = "Sports";
String car = "Mitsubishi";
String book = "War And Peace";
String movie = "Titanic";
System.out.println("My name is: " + name);
System.out.println("My favorite hobby is: " + hobby);
System.out.println("My favorite car is: " + car);
System.out.println("My favorite book is: " + book);
System.out.println("My favorite movie is: " + movie);
}
}
A Program that uses JOptionPane
Write a
java program that asks you to enter the cost of an item, how many of the items are available in inventory, the expiration month of the item. You must use JOptionPane to enter the 3 fields.
After you enter them, display each answer with JOptionPane with a separate dialog box.
Code
import javax.swing.*;
public class Program2 {
public static void main(String[] args) {
String cost = JOptionPane.showInputDialog("Please, enter cost of the item");
String numberOfItems = JOptionPane.showInputDialog("Please, enter how many items are there in inventory");
String expirationMonth = JOptionPane.showInputDialog("Please, enter expiration month");
JOptionPane.showMessageDialog(null, "Cost of the item is: " + cost);
JOptionPane.showMessageDialog(null, "There are " + numberOfItems + " items in inventory");
JOptionPane.showMessageDialog(null, "Item's expiration date is: " + expirationMonth);
}
}
A Program that uses Scanner
Write a java program that asks you to use Scanner enter the length of a single room in feet and to enter the width of the room in feet.
Assuming that the carpet they plan to use is $20.00 a square yard, compute how much it is going to cost to carpet the room. Then use println to print on 3 separate lines:
The number of square feet
The number of square yards
The cost to carpet the room.
Code
import javax.swing.*;
import java.util.Scanner;
public class Program3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double costPerSquareYard = 20;
System.out.print("Please, enter room length in feet: ");
double roomLength = Double.parseDouble(scanner.nextLine());
System.out.print("Please, enter room width in feet: ");
double roomWidth = Double.parseDouble(scanner.nextLine());
double sqFeet = roomWidth * roomLength;
System.out.println("Area of room in square feet: " + sqFeet);
double sqYards = sqFeet / 9;
System.out.println("Area of room in square yards: " + sqYards);
double cost = sqYards * costPerSquareYard;
System.out.print("The cost to carpet the room: " + cost);
}
}