Pages

Total Pageviews

Tuesday, July 2, 2019

Project #3 at Java Programming

import java.lang.Math;
import java.util.Scanner;
// This program was made by Erdogan Akbiyik

/*
Project 3 Fibonacci Program - The Fibonacci sequence is a sequence of numbers that result from the collection of each number with its predecessor.
In this sequence, when the numbers are proportional to each other, the golden ratio is revealed, that is, when a number is divided by the number before
it, a sequence is obtained that is getting closer to the golden ratio.
*/

class Fibonacci
{
   public static int calculateFibonacci(int n)
   {
      double v = (Math.pow((1.0+Math.sqrt(5.0))/2.0,n)-Math.pow((1.0-Math.sqrt(5.0))/2.0,n))/Math.sqrt(5.0);
      return (int)v;
   }
   public static void main(String[] args)
   {
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Welcome to Fibonacci Calculator");
      System.out.println("Please enter upper and lower bounds for range of Fibonacci values:");

      System.out.print("Lower: ");
      int lowerBound = keyboard.nextInt();
      System.out.print("Upper: ");
      int upperBound = keyboard.nextInt();
      System.out.println("Your values are:");

      for(int i = lowerBound;i<=upperBound;i++)
      {
         int value = calculateFibonacci(i);
         System.out.println(value);
      }
      System.out.println("Thanks for using my program!");

   }
}

// Sample Output:
// Fibonacci.java

// Welcome message
// Please enter upper and lower bounds for range of Fibonacci values:
// Lower:6
// Upper:12
// Your values are:
// 8
// 13
// 21
// 34
// 55
// 89
// 144
// Thanks for using program!


import java.util.Scanner;
// This program was made by Erdogan Akbiyik

/*
Project 3 PickWord Program - The purpose of this program is to find the numerical value of the word we want to find from the entered phrase.
*/

class PickWord {
   public static String pickAWord(String phrase, int index)
   {
      // While we cannot use String.split and StringTokenizer, we use for loop to find words in the phrase
      // We look up for the space character and we also control for multiple spaces int the phrase
      int currIndex = 0;
      String currWord = "";
      String lastWord = "";
      for(int i = 0; i<phrase.length();i++)
      {
         // Multiple spaces controlled and not count as a word
         if(phrase.charAt(i) == ' ')
         {         
            if(currWord != "")
            {
               //We found a word
               currIndex++;
               if(index <= currIndex) // We are going to cover zero and negative values
               {
                  return currWord;     // returning the value if its in the current index
               }
               lastWord = currWord;
               currWord = ""; 
            }       
         }
         else
         {
            currWord += phrase.charAt(i);   // we concantenate the charachter to word
         }
      }
      // If the index is greater than the word count just return the last word;
      return lastWord;
   }

   public static void main( String [] args) {
   
      Scanner phraseScanner = new Scanner(System.in);
      phraseScanner.useDelimiter ("\n");
      Scanner simpleScanner = new Scanner(System.in);
   
      System.out.println("Welcome to Word Picker");
      String response = "";
      do{
         System.out.println("Enter a phrase below please: ");
         String phrase = phraseScanner.next();
         System.out.print("Enter an index to pick word from the phrase: ");
         int index = simpleScanner.nextInt();
         String word = pickAWord(phrase, index);
         System.out.println("Picked word is : " + word);
         do {       
            System.out.print("Do you want to try again ? [y/n]: ");
            response = phraseScanner.next();
         } while(!(response.equals("y")||response.equals("n"))); //control user to enter y or n if not ask again
       
      } while(response.equals("y")); // if "y" then continue to run the program
      System.out.println("Thanks for using my program!");
   }
}   



import java.util.Scanner;
import java.lang.Math;
// This program was made by Erdogan Akbiyik

/*
Project 3 Triangle Program - The purpose of this program is created to be used to find the hypotenuse, perimeter and area of a triangle.
*/

class Triangle {
   public static double calculateArea(double a, double b)
   {
      double area = (a*b)/2.0;
      return area;
   }
   public static double calculateHypotenuse(double a, double b)
   {
      double hypotenuse = Math.sqrt(a*a+b*b);
      return hypotenuse;
   }
   public static double calculatePerimeter(double a, double b)
   {
      double hypotenuse = calculateHypotenuse(a,b);
      double perimeter = a+b+hypotenuse;
      return perimeter;
   }
   public static void main( String [] args) {
   
   
      Scanner scanner = new Scanner(System.in);
   
      System.out.println("Welcome to Right Triangle Analyzer");

      String response = "";
      do{
       
         System.out.println("Please enter the two smaller sides of the triangle as 'a' and 'b'");
         System.out.print("a : ");
         double a = scanner.nextDouble();
         System.out.print("b : ");
         double b = scanner.nextDouble();       
       
         double area = calculateArea(a,b);
         double hypotenuse = calculateHypotenuse(a,b);
         double perimeter = calculatePerimeter(a,b);
         System.out.println("Analysis of the triangle show below:");
         System.out.println("* Area: " + area);
         System.out.println("* Hypotenuse: " + hypotenuse);
         System.out.println("* Perimeter: " + perimeter);
       
         do {       
            System.out.print("Do you want to calculate again ? [y/n]: ");
            response = scanner.next();
         } while(!(response.equals("y")||response.equals("n"))); //control user to enter y or n if not ask again
       
      } while(response.equals("y")); // if "y" then continue to run the program
      System.out.println("Thanks for using my program!");
   }
 
}   



http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg

No comments:

Post a Comment