Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Qaiyum Mohammmad | 2018.09.26 */
- /* Problem Set 2, all questions */
- import java.util.*;
- import java.text.DecimalFormat;
- class Main {
- public static void main(String[] args) {
- // new scanners
- Scanner sc = new Scanner(System.in);
- // this ones for strings only
- Scanner str = new Scanner(System.in);
- DecimalFormat dec = new DecimalFormat (".###");
- // Problem Set 2 #1
- /* Ask the user for a number and print that many '*'
- characters in a vertical line. */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #1");
- System.out.println ("__________________");
- System.out.println ("");
- // declare variables
- int asteriskNum;
- double rectHeight;
- double rectWidth;
- // asks for number
- System.out.print ("How many asterisks do you want? (please insert whole number) : ");
- asteriskNum = sc.nextInt();
- /* while asteriskNum is greater than 0, an asterisk will print. However, each
- loop will also decrease the value of asteriskNum by 1 */
- while ( asteriskNum > 0 )
- {
- System.out.println ("*");
- asteriskNum--;
- }
- // Problem Set 2 #2
- /* Ask the user for a number and print that many '*'
- characters in a horizontal line. */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #2");
- System.out.println ("__________________");
- System.out.println ("");
- // asteriskNum has been previously declared
- // asks for number
- System.out.print ("How many asterisks do you want? (please insert whole number) : ");
- asteriskNum = sc.nextInt();
- /* while asteriskNum is greater than 0, an asterisk will print. However, each
- loop will also decrease the value of asteriskNum by 1 */
- while ( asteriskNum > 0 )
- {
- System.out.print ("*");
- asteriskNum--;
- }
- // Problem Set 2 #3
- /* Ask a user for a number and
- print a solid square of '*' characters. */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #3");
- System.out.println ("__________________");
- System.out.println ("");
- // asteriskNum has been previously declared
- // asks for number
- System.out.print ("How many asterisks do you want? (please insert whole number) : ");
- asteriskNum = sc.nextInt();
- /* while asteriskNum is greater than 0, an asterisk will print. However, each
- loop will also decrease the value of asteriskNum by 1 */
- /* this same rule also applies to how many times the line of asterisk will print,
- which is the same as how many asterisks in a single line since a square has the
- same width as it does length */
- for (int i=asteriskNum; i>0; i--)
- {
- for (int j=asteriskNum; j>0; j--)
- {
- System.out.print("*");
- }
- System.out.print("\n");
- }
- // Problem Set 2 #4
- /* Your teacher needs help calculating the class average.
- Your program will ask how many students are in the class,
- and it will then accept that many number of grades,
- and calculate the class average. */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #4");
- System.out.println ("__________________");
- System.out.println ("");
- // declare variables
- double grade = 0;
- int numStudent = 0;
- double numGrade = 0;
- // asks for the number of students
- System.out.println("Please enter the total number of students: ");
- numStudent = sc.nextInt();
- /* k = number of students. When the number of students is
- greater than 0, it will decrease k by 1 each loop.
- Each loop asks for a grade and adds the value of the
- grades onto the variable numGrade
- */
- for (int k = numStudent; k >0; k--) {
- System.out.println("Please enter the students' grades: ");
- grade = sc.nextDouble();
- numGrade += grade;
- }
- // prints the average as numGrade divided by number of students
- System.out.println("The class average is: ");
- System.out.print (numGrade/numStudent);
- // Problem Set 2 #5
- /* Write a password checker. Your password checker will first ask for a password,
- then repetitively ask the user to guess at the password. When
- the user types the same password, acknowledge that the right
- password was provided. DO NOT USE AN IF STATEMENT. */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #5");
- System.out.println ("__________________");
- System.out.println ("");
- // declare variables
- String password;
- String userGuess;
- // asks for a password
- System.out.print ("Enter a password:");
- password = str.nextLine();
- // keeps guessing until guess = password
- do {
- System.out.print ("Enter a guess:");
- userGuess = str.nextLine();
- }
- while (!password.equals (userGuess));
- System.out.println ("Correct Password");
- // Problem Set 2 #6
- /* Write a program that asks for a numeric range. For all
- the numbers in that range, calculate and display the square,
- square root and cube of the numbers. Keep all numbers to 3
- decimal points */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #6");
- System.out.println ("__________________");
- System.out.println ("");
- // declare variables
- double rangeEnd;
- double rangeStart;
- // asks for the range
- System.out.print ("Enter the start of the range:");
- rangeStart = sc.nextDouble();
- System.out.print ("Enter the end of the range:");
- rangeEnd = sc.nextDouble();
- System.out.println ("");
- System.out.println ("FORMAT IS THE FOLLOWING:");
- System.out.println ("Number, square, square root, cube");
- while ( rangeEnd >= rangeStart)
- {
- System.out.println ("");
- System.out.print (rangeEnd); // prints the number
- System.out.print (", ");
- System.out.print (Math.pow(rangeEnd, 2)); // prints number^2
- System.out.print (", ");
- System.out.print ((dec.format(Math.sqrt(rangeEnd)))); // prints sqrtroot of number
- System.out.print (", ");
- System.out.print (Math.pow(rangeEnd, 3)); //prints number ^3
- rangeEnd--;
- }
- // Problem Set 2 #7
- /* Write a program which will decide if a number is even or odd,
- positive, negative or 0. Output all necessary information */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #7");
- System.out.println ("__________________");
- System.out.println ("");
- // declare variables
- int numUser;
- double numRemainder;
- System.out.println ("Enter a whole number:");
- numUser = sc.nextInt();
- // Checks is the number is even (or disvisible by two)
- numRemainder = numUser % 2;
- // Checks if the number is zero
- if (numUser == 0) {
- System.out.println ("That number is equal to 0");
- }
- // This checks if the number is positive
- else if (numUser > 0){
- System.out.println ("That number is positive");
- }
- else {
- // This checks that if it isn't positive or zero, it's negative
- System.out.println ("That number is negative");
- }
- // Checks if the number is even
- if (numRemainder == 0) {
- System.out.println ("Your number is even.");
- }
- else { // If it's not even, the number is odd.
- System.out.println ("Your number is odd.");
- }
- // Problem Set 2 #8
- /* determine whether a year is a leap year */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #8");
- System.out.println ("__________________");
- System.out.println ("");
- int year ;
- boolean yearLeap ;
- System.out.println ("Enter a year:");
- year = sc.nextInt();
- // divisible by 4
- yearLeap = (year % 4 == 0);
- // divisible by 4, but not 100
- yearLeap = yearLeap && (year % 100 != 0);
- // divisible by 4 but not 100 unless divisible by 400
- yearLeap = yearLeap || (year % 400 == 0);
- System.out.println("This year is a leap year:");
- System.out.println(yearLeap);
- // Problem Set 2 #9
- /* Determine the astrological sign given the month and
- day of the person’s birthday */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #9");
- System.out.println ("__________________");
- System.out.println ("");
- // declares variables
- int month;
- int day;
- System.out.println ("Enter your birth month in number format (ex: January = 01) :");
- month = sc.nextInt();
- System.out.println ("Enter your birth year:");
- day = sc.nextInt();
- System.out.println("Your sign is:");
- // if your birthday is between xx.xx OR xx.xx you are XXXXXX
- if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19))
- System.out.println("Capricorn");
- else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17))
- System.out.println("Aquarius");
- else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19))
- System.out.println("Pisces");
- else if ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19))
- System.out.println("Aries");
- else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20))
- System.out.println("Taurus");
- else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20))
- System.out.println("Gemini");
- else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22))
- System.out.println("Cancer");
- else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22))
- System.out.println("Leo");
- else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22))
- System.out.println("Virgo");
- else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22))
- System.out.println("Libra");
- else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21))
- System.out.println("Scorpio");
- else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21))
- System.out.println("Sagittarius");
- // date was not entered correctly
- else
- System.out.println("You entered your birthday incorrectly. Skipping program.");
- // Problem Set 2 #10
- /* Credit card application evaluation. Based on the input values, points are obtained.
- Based on the total points, various actions are taken.y */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #10");
- System.out.println ("__________________");
- System.out.println ("");
- // Declare variables
- double creditAge, creditCurrent, creditIncome, workCurrent;
- int creditPoints;
- // asks for age
- System.out.print ("How old are you? ");
- creditAge = sc.nextDouble ();
- // asks how long you lived in your home
- System.out.print ("How long have you lived in your current place of residence?");
- creditCurrent = sc.nextDouble ();
- // asks for annual income
- System.out.print ("Please enter your annual income: ");
- creditIncome = sc.nextDouble ();
- // asks how long they had their job
- System.out.print ("For how long have you been working your current job? ");
- workCurrent = sc.nextDouble ();
- // if age is under 20, deduct 10 points
- if (20 >= creditAge) {
- creditPoints = -10;
- }
- // if age is between 21 and 30, leave points unchanged
- else if (21 <= creditAge && 30 >= creditAge) {
- creditPoints = 0;
- }
- // if age is between 31 and 50, allott 20 points
- else if (31 <= creditAge && 50 >= creditAge) {
- creditPoints = 20;
- }
- // if age is 50+, allot 25 points
- else {
- creditPoints = 25;
- }
- // if they lived in their home for less than 1 years, deduct 5 points
- if (1 > creditCurrent) {
- creditPoints += -5;
- }
- // if they lived in their home for 1-3 years, allot 5 points
- else if (1 <= creditCurrent && 3 >= creditCurrent) {
- creditPoints += 5;
- }
- // if they lived in their home for 4-8 years, allot 12 points
- else if (4 <= workCurrent && 8 >= workCurrent) {
- creditPoints += 12;
- }
- // if they lived in their home for 8+ years, allot 20 points
- else {
- creditPoints += 20;
- }
- if (15000 > creditIncome) {
- creditPoints += 0;
- }
- else if (15000 <= creditIncome && 25000 > creditIncome) {
- creditPoints += 12;
- }
- else if (25000 <= creditIncome && 40000 >= creditIncome) {
- creditPoints += 24;
- }
- else {
- creditPoints += 30;
- }
- if (2 > workCurrent) {
- creditPoints += -4;
- }
- else if (2 <= workCurrent && 4 >= workCurrent) {
- creditPoints += 8;
- }
- else {
- creditPoints += 15;
- }
- // prints results
- if (20 >= creditPoints) {
- System.out.println ("Sorry, you are not eligible for a credit card.");
- }
- else if (21 <= creditPoints && 35 >= creditPoints) {
- System.out.println ("You are eligible for a credit card with a $500 limit.");
- }
- else if (36 <= creditPoints && 60 >= creditPoints) {
- System.out.println ("You are eligible for a credit card with a $2000 limit.");
- }
- else {
- System.out.println ("You are eligible for a credit card with a $5000 limit.");
- }
- // Problem Set 2 #11
- /* Postage problem. Given the weight of a letter in grams, output the cost of the postage: */
- System.out.println ("");
- System.out.println ("__________________");
- System.out.println ("Problem Set 2 #11");
- System.out.println ("__________________");
- System.out.println ("");
- // Declare variables
- double weight, cost;
- // User inputs the weight of their letter
- System.out.print ("Enter the weight of your letter (in grams): ");
- weight = sc.nextDouble ();
- if (30 >= weight) {
- System.out.println ("The cost of postage is 48 ¢.");
- }
- else if (50 >= weight) {
- System.out.println ("The cost of postage is 70 ¢.");
- }
- else if (100 >= weight) {
- System.out.println ("The cost of postage is 90 ¢.");
- }
- else {
- weight = (weight-100);
- weight = (weight/50);
- weight = Math.ceil(weight);
- cost = 90 + (weight*18);
- if (100 <= cost) {
- cost = cost/100;
- System.out.println ("The cost of postage is $" + dec.format(cost) + ".");
- }
- else {
- System.out.println ("The cost of postage is " + dec.format(cost) + " ¢.");
- } }
- // god damn qaiyum stop touching these brackets
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement