Question

In: Computer Science

/* Problem 1 * Write and run a java program that prints out two things you...

/*      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

Solutions

Expert Solution

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:


Related Solutions

Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Description: You are to develop a Java program that prints out the multiplication or addition table...
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used. You are free to add more methods as you see fit – but all the methods listed below must be used in your solution. For the class Table the following methods are required: - Protected Constructor – stores the start and size of table, creates the...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a Java method called printAvg that takes in two floating point numbers and prints out...
Write a Java method called printAvg that takes in two floating point numbers and prints out the average of them.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT