Advertisement
Guest User

Untitled

a guest
Oct 1st, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.93 KB | None | 0 0
  1. /* Qaiyum Mohammmad | 2018.09.26 */
  2. /* Problem Set 2, all questions */
  3.  
  4. import java.util.*;
  5. import java.text.DecimalFormat;
  6.  
  7. class Main {
  8.   public static void main(String[] args) {
  9.    
  10.      // new scanners
  11.   Scanner sc = new Scanner(System.in);
  12.      // this ones for strings only
  13.   Scanner str = new Scanner(System.in);
  14.  
  15.   DecimalFormat dec = new DecimalFormat (".###");
  16.    
  17.    
  18.  
  19.  
  20. // Problem Set 2 #1
  21.   /* Ask the user for a number and print that many '*'
  22.   characters in a vertical line. */
  23.  
  24.   System.out.println ("");
  25.   System.out.println ("__________________");
  26.   System.out.println ("Problem Set 2 #1");
  27.   System.out.println ("__________________");
  28.   System.out.println ("");
  29.  
  30.   // declare variables
  31.   int  asteriskNum;
  32.   double  rectHeight;
  33.   double  rectWidth;
  34.  
  35.   // asks for number
  36.   System.out.print ("How many asterisks do you want? (please insert whole number) : ");
  37.   asteriskNum = sc.nextInt();
  38.  
  39.  
  40.   /* while asteriskNum is greater than 0, an asterisk will print. However, each
  41.    loop will also decrease the value of asteriskNum by 1 */
  42.   while ( asteriskNum > 0 )
  43.   {
  44.    
  45.     System.out.println ("*");
  46.     asteriskNum--;
  47.   }
  48.  
  49. // Problem Set 2 #2
  50.   /* Ask the user for a number and print that many '*'
  51.   characters in a horizontal line.   */
  52.  
  53.   System.out.println ("");
  54.   System.out.println ("__________________");
  55.   System.out.println ("Problem Set 2 #2");
  56.   System.out.println ("__________________");
  57.   System.out.println ("");
  58.  
  59.   // asteriskNum has been previously declared
  60.  
  61.   // asks for number
  62.   System.out.print ("How many asterisks do you want? (please insert whole number) : ");
  63.   asteriskNum = sc.nextInt();
  64.  
  65.   /* while asteriskNum is greater than 0, an asterisk will print. However, each
  66.    loop will also decrease the value of asteriskNum by 1 */
  67.    while ( asteriskNum > 0 )
  68.   {
  69.    
  70.  
  71.     System.out.print ("*");
  72.     asteriskNum--;
  73.   }
  74.  
  75. // Problem Set 2 #3
  76.   /* Ask a user for a number and
  77.   print a solid square of '*' characters.   */
  78.  
  79.   System.out.println ("");
  80.   System.out.println ("__________________");
  81.   System.out.println ("Problem Set 2 #3");
  82.   System.out.println ("__________________");
  83.   System.out.println ("");
  84.  
  85.   // asteriskNum has been previously declared
  86.  
  87.   // asks for number
  88.   System.out.print ("How many asterisks do you want? (please insert whole number) : ");
  89.   asteriskNum = sc.nextInt();
  90.  
  91.   /* while asteriskNum is greater than 0, an asterisk will print. However, each
  92.    loop will also decrease the value of asteriskNum by 1 */
  93.    
  94.   /* this same rule also applies to how many times the line of asterisk will print,
  95.   which is the same as how many asterisks in a single line since a square has the
  96.   same width as it does length */
  97.  
  98.   for (int i=asteriskNum; i>0; i--)
  99.   {
  100.    for (int j=asteriskNum; j>0; j--)
  101.    {
  102.        System.out.print("*");
  103.    }
  104.    System.out.print("\n");
  105.   }
  106.    
  107.  
  108. // Problem Set 2 #4
  109.   /* Your teacher needs help calculating the class average.  
  110.   Your program will ask how many students are in the class,
  111.   and it will then accept that many number of grades,
  112.   and calculate the class average.   */
  113.  
  114.   System.out.println ("");
  115.   System.out.println ("__________________");
  116.   System.out.println ("Problem Set 2 #4");
  117.   System.out.println ("__________________");  
  118.   System.out.println ("");
  119.  
  120.   // declare variables
  121.   double grade = 0;
  122.   int numStudent = 0;
  123.   double numGrade = 0;
  124.  
  125.  
  126.   // asks for the number of students
  127.   System.out.println("Please enter the total number of students: ");
  128.   numStudent = sc.nextInt();
  129.  
  130.  
  131.   /* k = number of students. When the number of students is
  132.   greater than 0, it will decrease k by 1 each loop.
  133.   Each loop asks for a grade and adds the value of the
  134.   grades onto the variable numGrade
  135.   */
  136.    for (int k = numStudent; k >0; k--) {
  137.             System.out.println("Please enter the students' grades: "); 
  138.             grade = sc.nextDouble();
  139.             numGrade += grade;
  140.             }
  141.            
  142.    
  143.   // prints the average as numGrade divided by number of students        
  144.   System.out.println("The class average is: ");
  145.   System.out.print (numGrade/numStudent);
  146.  
  147. // Problem Set 2 #5
  148.   /* Write a password checker. Your password checker will first ask for a password,
  149.   then repetitively ask the user to guess at the password. When
  150.   the user types the same password, acknowledge that the right
  151.   password was provided. DO NOT USE AN IF STATEMENT. */
  152.  
  153.   System.out.println ("");
  154.   System.out.println ("__________________");
  155.   System.out.println ("Problem Set 2 #5");
  156.   System.out.println ("__________________");
  157.   System.out.println ("");
  158.  
  159.   // declare variables
  160.   String password;
  161.   String userGuess;
  162.  
  163.   // asks for a password
  164.   System.out.print ("Enter a password:");
  165.   password = str.nextLine();
  166.  
  167.   // keeps guessing until guess = password
  168.   do {
  169.     System.out.print ("Enter a guess:");
  170.     userGuess = str.nextLine();
  171.    
  172.   }
  173.  
  174.   while (!password.equals (userGuess));
  175.   System.out.println ("Correct Password");
  176.  
  177. // Problem Set 2 #6
  178.   /* Write a program that asks for a numeric range.  For all
  179.   the numbers in that range, calculate and display  the square,
  180.   square root and cube of the numbers.  Keep all numbers to 3
  181.   decimal points */
  182.  
  183.   System.out.println ("");
  184.   System.out.println ("__________________");
  185.   System.out.println ("Problem Set 2 #6");
  186.   System.out.println ("__________________");
  187.   System.out.println ("");
  188.  
  189.   // declare variables
  190.   double rangeEnd;
  191.   double rangeStart;
  192.  
  193.   // asks for the range
  194.   System.out.print ("Enter the start of the range:");
  195.   rangeStart = sc.nextDouble();
  196.  
  197.   System.out.print ("Enter the end of the range:");
  198.   rangeEnd = sc.nextDouble();
  199.  
  200.   System.out.println ("");
  201.  
  202.   System.out.println ("FORMAT IS THE FOLLOWING:");
  203.   System.out.println ("Number, square, square root, cube");
  204.  
  205.   while ( rangeEnd >= rangeStart)
  206.   {
  207.     System.out.println ("");
  208.  
  209.     System.out.print (rangeEnd); // prints the number
  210.     System.out.print (", ");
  211.     System.out.print (Math.pow(rangeEnd, 2)); // prints number^2
  212.     System.out.print (", ");
  213.     System.out.print ((dec.format(Math.sqrt(rangeEnd)))); // prints sqrtroot of number
  214.     System.out.print (", ");
  215.     System.out.print (Math.pow(rangeEnd, 3)); //prints number ^3
  216.     rangeEnd--;
  217.    
  218.   }
  219.  
  220. // Problem Set 2 #7
  221.   /* Write a program which will decide if a number is even or odd,
  222.   positive, negative or 0.  Output all necessary information */
  223.  
  224.   System.out.println ("");
  225.   System.out.println ("__________________");
  226.   System.out.println ("Problem Set 2 #7");
  227.   System.out.println ("__________________");
  228.   System.out.println ("");
  229.  
  230.   // declare variables
  231.  
  232.   int numUser;
  233.   double numRemainder;
  234.  
  235.   System.out.println ("Enter a whole number:");
  236.   numUser = sc.nextInt();
  237.  
  238.   // Checks is the number is even (or disvisible by two)
  239.   numRemainder = numUser % 2;
  240.  
  241.   // Checks if the number is zero
  242.   if (numUser == 0) {    
  243.     System.out.println ("That number is equal to 0");
  244.    
  245.   }
  246.  
  247.   // This checks if the number is positive
  248.   else if (numUser > 0){  
  249.     System.out.println ("That number is positive");
  250.   }
  251.   else {    
  252.   // This checks that if it isn't positive or zero, it's negative
  253.     System.out.println ("That number is negative");
  254.   }
  255.  
  256.  
  257.   // Checks if the number is even
  258.   if (numRemainder == 0) {  
  259.     System.out.println ("Your number is even.");
  260.   }
  261.   else {    // If it's not even, the number is odd.
  262.     System.out.println ("Your number is odd.");
  263.   }
  264.  
  265. // Problem Set 2 #8
  266.   /* determine whether a year is a leap year */
  267.  
  268.   System.out.println ("");
  269.   System.out.println ("__________________");
  270.   System.out.println ("Problem Set 2 #8");
  271.   System.out.println ("__________________");
  272.   System.out.println ("");
  273.  
  274.  int year ;
  275.  boolean yearLeap ;
  276.  
  277.  System.out.println ("Enter a year:");
  278.  year = sc.nextInt();
  279.  
  280.  // divisible by 4
  281.  yearLeap = (year % 4 == 0);
  282.  
  283.  // divisible by 4, but not 100
  284.  yearLeap = yearLeap && (year % 100 != 0);
  285.  
  286.  // divisible by 4 but not 100 unless divisible by 400
  287.  yearLeap = yearLeap || (year % 400 == 0);
  288.  
  289.  System.out.println("This year is a leap year:");
  290.  System.out.println(yearLeap);
  291.  
  292. // Problem Set 2 #9
  293.   /* Determine the astrological sign given the month and
  294.   day of the person’s birthday */
  295.  
  296.   System.out.println ("");
  297.   System.out.println ("__________________");
  298.   System.out.println ("Problem Set 2 #9");
  299.   System.out.println ("__________________");
  300.   System.out.println ("");
  301.  
  302.   // declares variables
  303.   int month;
  304.   int day;
  305.  
  306.   System.out.println ("Enter your birth month in number format (ex: January = 01) :");
  307.   month = sc.nextInt();
  308.  
  309.   System.out.println ("Enter your birth year:");
  310.   day = sc.nextInt();
  311.  
  312.     System.out.println("Your sign is:");
  313.  
  314.   // if your birthday is between xx.xx OR xx.xx you are XXXXXX
  315.  
  316.   if      ((month == 12 && day >= 22 && day <= 31) || (month ==  1 && day >= 1 && day <= 19))
  317.   System.out.println("Capricorn");
  318.   else if ((month ==  1 && day >= 20 && day <= 31) || (month ==  2 && day >= 1 && day <= 17))
  319.   System.out.println("Aquarius");
  320.   else if ((month ==  2 && day >= 18 && day <= 29) || (month ==  3 && day >= 1 && day <= 19))
  321.   System.out.println("Pisces");
  322.   else if ((month ==  3 && day >= 20 && day <= 31) || (month ==  4 && day >= 1 && day <= 19))
  323.   System.out.println("Aries");
  324.   else if ((month ==  4 && day >= 20 && day <= 30) || (month ==  5 && day >= 1 && day <= 20))
  325.   System.out.println("Taurus");
  326.   else if ((month ==  5 && day >= 21 && day <= 31) || (month ==  6 && day >= 1 && day <= 20))
  327.   System.out.println("Gemini");
  328.   else if ((month ==  6 && day >= 21 && day <= 30) || (month ==  7 && day >= 1 && day <= 22))
  329.   System.out.println("Cancer");
  330.   else if ((month ==  7 && day >= 23 && day <= 31) || (month ==  8 && day >= 1 && day <= 22))
  331.   System.out.println("Leo");
  332.   else if ((month ==  8 && day >= 23 && day <= 31) || (month ==  9 && day >= 1 && day <= 22))
  333.   System.out.println("Virgo");
  334.   else if ((month ==  9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22))
  335.   System.out.println("Libra");
  336.   else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21))
  337.   System.out.println("Scorpio");
  338.   else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21))
  339.   System.out.println("Sagittarius");
  340.  
  341.   // date was not entered correctly
  342.   else
  343.   System.out.println("You entered your birthday incorrectly. Skipping program.");
  344.  
  345. // Problem Set 2 #10
  346.   /*  Credit card application evaluation. Based on the input values, points are obtained.
  347.   Based on the total points, various actions are taken.y */
  348.  
  349.   System.out.println ("");
  350.   System.out.println ("__________________");
  351.   System.out.println ("Problem Set 2 #10");
  352.   System.out.println ("__________________");
  353.   System.out.println ("");
  354.  
  355.   // Declare variables
  356.   double creditAge, creditCurrent, creditIncome, workCurrent;
  357.   int creditPoints;
  358.  
  359.   // asks for age
  360.   System.out.print ("How old are you? ");
  361.   creditAge = sc.nextDouble ();
  362.  
  363.   // asks how long you lived in your home
  364.   System.out.print ("How long have you lived in your current place of residence?");
  365.   creditCurrent = sc.nextDouble ();
  366.  
  367.   // asks for annual income
  368.   System.out.print ("Please enter your annual income: ");
  369.   creditIncome = sc.nextDouble ();
  370.  
  371.    // asks how long they had their job
  372.   System.out.print ("For how long have you been working your current job? ");
  373.   workCurrent = sc.nextDouble ();
  374.  
  375.   // if age is under 20, deduct 10 points
  376.   if (20 >= creditAge) {      
  377.     creditPoints = -10;
  378.   }
  379.  
  380.   // if age is between 21 and 30, leave points unchanged
  381.     else if (21 <= creditAge && 30 >= creditAge) {    
  382.       creditPoints = 0;
  383.     }
  384.  
  385.   // if age is between 31 and 50, allott 20 points
  386.     else if (31 <= creditAge && 50 >= creditAge) {    
  387.       creditPoints = 20;
  388.     }
  389.    
  390.   // if age is 50+, allot 25 points
  391.     else {    
  392.       creditPoints = 25;
  393.     }
  394.  
  395.   // if they lived in their home for less than 1 years, deduct 5 points
  396.   if (1 > creditCurrent) {    
  397.     creditPoints += -5;
  398.   }
  399.  
  400.   // if they lived in their home for 1-3 years, allot 5 points
  401.     else if (1 <= creditCurrent && 3 >= creditCurrent) {    
  402.       creditPoints += 5;
  403.     }
  404.  
  405.   // if they lived in their home for 4-8 years, allot 12 points
  406.     else if (4 <= workCurrent && 8 >= workCurrent) {    
  407.       creditPoints += 12;
  408.     }
  409.  
  410.   // if they lived in their home for 8+ years, allot 20 points
  411.     else {    
  412.       creditPoints += 20;
  413.     }
  414.  
  415.   if (15000 > creditIncome) {    
  416.     creditPoints += 0;
  417.   }
  418.     else if (15000 <= creditIncome && 25000 > creditIncome) {
  419.       creditPoints += 12;
  420.     }
  421.     else if (25000 <= creditIncome && 40000 >= creditIncome) {
  422.       creditPoints += 24;
  423.     }
  424.     else {
  425.       creditPoints += 30;
  426.     }
  427.  
  428.   if (2 > workCurrent) {
  429.     creditPoints += -4;
  430.   }
  431.     else if (2 <= workCurrent && 4 >= workCurrent) {
  432.       creditPoints += 8;
  433.     }
  434.     else {
  435.       creditPoints += 15;
  436.     }
  437.  
  438.  
  439.   // prints results
  440.   if (20 >= creditPoints) {
  441.     System.out.println ("Sorry, you are not eligible for a credit card.");
  442.   }
  443.     else if (21 <= creditPoints && 35 >= creditPoints) {
  444.       System.out.println ("You are eligible for a credit card with a $500 limit.");
  445.     }
  446.     else if (36 <= creditPoints && 60 >= creditPoints) {
  447.       System.out.println ("You are eligible for a credit card with a $2000 limit.");
  448.     }
  449.     else {
  450.       System.out.println ("You are eligible for a credit card with a $5000 limit.");
  451.     }
  452.  
  453.  
  454.   // Problem Set 2 #11
  455.   /*   Postage problem. Given the weight of a letter in grams, output the cost of the postage: */
  456.  
  457.   System.out.println ("");
  458.   System.out.println ("__________________");
  459.   System.out.println ("Problem Set 2 #11");
  460.   System.out.println ("__________________");
  461.   System.out.println ("");
  462.  
  463.  
  464.   // Declare variables
  465.   double weight, cost;
  466.  
  467.     // User inputs the weight of their letter
  468.   System.out.print ("Enter the weight of your letter (in grams): ");
  469.   weight = sc.nextDouble ();
  470.  
  471.   if (30 >= weight) {
  472.     System.out.println ("The cost of postage is 48 ¢.");
  473.   }
  474.  
  475.     else if (50 >= weight) {
  476.       System.out.println ("The cost of postage is 70 ¢.");
  477.     }
  478.     else if (100 >= weight) {
  479.       System.out.println ("The cost of postage is 90 ¢.");
  480.     }
  481.     else {
  482.       weight = (weight-100);
  483.       weight = (weight/50);
  484.       weight = Math.ceil(weight);
  485.       cost = 90 + (weight*18);
  486.      
  487.       if (100 <= cost) {
  488.         cost = cost/100;
  489.         System.out.println ("The cost of postage is $" + dec.format(cost) + ".");
  490.       }
  491.       else {
  492.       System.out.println ("The cost of postage is " + dec.format(cost) + " ¢.");
  493.     } }
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501. // god damn qaiyum stop touching these brackets
  502.   }
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement