In: Computer Science
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a program that prompts the user to enter the base and height of a triangle. * The program then calculates the area of the triangle. * The program also prompts the user to enter the length and width of a rectangle. * The program then calculates the perimeter of the rectangle. * Your code must print out: the base of the triangle, the width of the triangle, the area * of the triangle, the length of the rectangle, the width of the rectangle, and the area of the rectangle. * (5 Points.) */ /* Problem 3 * Write and run a java program that prompts you to enter your year of birth. * The program then returns your age by subtracting your year of birth from the * current year. The program must print: You are x years old, where x is your * age. You must import the scanner class (5 Points.) /* Problem 4 * Write a java program that asks the user to enter her\his first name, * middle initial, last name, class name, letter grade, and GPA. * The program then prints out the above items as follows. * Line 1: Your name is first name, middle initial, last name. * Line 2: You are enrolled in class name * Line 3: You scored letter grade ... in class name * Line 4: Your GPA in class name is GPA * Hint: To read characters from the scanner, you should use this syntax: * String firstName = input.nextLine(); * Note that you will use both String and double variable types.
* Note that we use Notepad ++ in our class for java and we also use cmd
Problem1:
//Program to be saved as Problem1.java
import java.util.*;
public class Problem1 {
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
String
thingslearned1,thingslearned2;
String
thingstobelearned1,thingstobelearned2,thingstobelearned3;
//read the things you have learnt
so far
System.out.println("\nEnter two
things you have learned so far in this class: ");
thingslearned1=input.nextLine();
thingslearned2=input.nextLine();
//read the things you hope to
learn
System.out.println("\nEnter three
things you hope to learn: ");
thingstobelearned1=input.nextLine();
thingstobelearned2=input.nextLine();
thingstobelearned3=input.nextLine();
//print out the things each in a
separate line using println
System.out.println("\nThe two
things you have learned so far in this class: ");
System.out.println(thingslearned1);
System.out.println(thingslearned2);
System.out.println("\nThe three
things you hope to learn: ");
System.out.println(thingstobelearned1);
System.out.println(thingstobelearned2);
System.out.println(thingstobelearned3);
}
}
Output:
Problem2:
//Program to be saved as Problem2.java
import java.util.*;
public class Problem2 {
public static void main(String[] args) {
double
baseOfTriangle,heightOfTriangle;
double
lengthOfRectangle,widthOfRectangle;
double
areaOfTriangle,perimeterOfRectangle;
Scanner input=new
Scanner(System.in);
//read the base of the
triangle
System.out.print("\nEnter the base
of the triangle: ");
baseOfTriangle=input.nextDouble();
//read the height of the
triangle
System.out.print("\nEnter the
height of the triangle: ");
heightOfTriangle=input.nextDouble();
//read the length of the
rectangle
System.out.print("\nEnter the
length of the rectangle: ");
lengthOfRectangle=input.nextDouble();
//read the width of the
rectangle
System.out.print("\nEnter the width
of the rectangle: ");
widthOfRectangle=input.nextDouble();
//calculate the area of the
triangle
areaOfTriangle =
(1.0/2.0)*(baseOfTriangle*heightOfTriangle);
//calculate the perimeter of the
rectangle
perimeterOfRectangle =
2*lengthOfRectangle*widthOfRectangle;
//print the results
System.out.println("\nThe base of
the triangle is: "+baseOfTriangle);
System.out.println("\nThe height of
the triangle is: "+heightOfTriangle);
System.out.println("\nThe area of
the triangle is: "+areaOfTriangle);
System.out.println("\n\nThe length
of the rectangle is: "+lengthOfRectangle);
System.out.println("\nThe width of
the rectangle is: "+widthOfRectangle);
System.out.println("\nThe perimeter
of the rectangle is: "+perimeterOfRectangle);
}
}
Output:
Problem3:
//Program to be saved as Problem3.java
import java.util.*;
public class Problem3 {
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
int yearOfBirth, age;
//read the year of birth
System.out.print("\nEnter your year
of birth: ");
yearOfBirth=input.nextInt();
//create a Calender object and
get the current calender date
Calendar calendar =
Calendar.getInstance();
//calculate age, get the current
year using calender object
age= calendar.get(calendar.YEAR) -
yearOfBirth;
//print the output
System.out.println("\nYou are
"+age+" years old.");
}
}
Output:
Problem4:
//Program to be saved as Problem3.java
import java.util.*;
public class Problem4 {
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
String firstname, middlename,
lastname, classname, lettergrade;
double GPA;
//asks the user to enter his
first name,middle initial, last name, class name, letter grade, and
GPA.
System.out.print("\nEnter
firstname: ");
firstname=input.nextLine();
//asks the user to enter his middle
initial
System.out.print("\nEnter
middlename: ");
middlename=input.nextLine();
//asks the user to enter his last
name
System.out.print("\nEnter lastname:
");
lastname=input.nextLine();
//asks the user to enter his class
name
System.out.print("\nEnter class
name: ");
classname=input.nextLine();
//asks the user to enter his letter
grade
System.out.print("\nEnter letter
grade: ");
lettergrade=input.nextLine();
//asks the user to enter his
GPA
System.out.print("\nEnter GPA:
");
GPA=input.nextDouble();
//print the details entered
System.out.println("\nYour name is
"+firstname+", "+middlename+", "+lastname);
System.out.println("You are
enrolled in "+classname);
System.out.println("You scored
letter grade "+lettergrade+" in "+classname);
System.out.println("Your GPA in
"+classname+" is "+GPA);
}
}
Output: