SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES

FORGOT YOUR PASSWORD?

FORGOT YOUR DETAILS?

AAH, WAIT, I REMEMBER NOW!
Openbaraza

Openbaraza

Openbaraza

Baraza is a Swahili word that means a deliberation meeting held by a collective group of people with wisdom.

T : +254 (20) 2243097 / 2227100
Email: info@openbaraza.org

OPENBARAZA
Dew CIS Solutions LTD Barclays Plaza, 12th Floor. Loita Street.

Open in Google Maps
  • Framework
  • Projects
    • Human Resource
      • Human Resource
      • Time Attendance
      • Project Planning
    • Finance Management
      • Business
      • Payroll
    • Investment Management
      • Banking
      • Chama
      • Sacco
    • Case Management
      • Judiciary
    • Real Estate Management
      • Property
    • Academic Management
      • UMIS
      • Schools
  • Forums
    • HCM HR Payroll
    • Business
    • Sacco
    • Banking
  • Blog & News
    • OpenBaraza Framework
    • Databases
    • Java Programming
    • Scrum
  • Contact Us
  • Home
  • Blog & News
  • Java Programming
  • 04. Control Structures
May 18, 2025

04. Control Structures

04. Control Structures

by dennis@dennis.me.ke / Wednesday, 26 June 2019 / Published in Java Programming

CONTROL STRUCTURES USING POWER SERIES OF SINE

Do you wish to have more control in your life? Of course you do. Well that’s a story for a another day. However,did you know you can control a computer however you please. We shall do this using java control structures.

Imagine giving instructions to a child to bathe. You tell him/her to go into the bathroom and use water and soap. In the same way we give a computer instructions. You forgot to tell the child to remove his clothes before entering the bathroom.That’s exactly how a computer follows instructions.

Let’s look at the definition of sine particularly its Power series. Can it be defined?

Yes. The Power series of sine is defines as:

 

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

LIVE A LITTLE

What did the Science book say to the Math book?

You have problems men

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

Let’s try computing this formular in java. Copy compile and run the following code:

 

 

Output:

 

 

Explanation:

import java.util.Scanner;

public class Condition {

//method that does factorial of the denominator

public static double factorial(double n) {

 

// base case

if (n <= 1)

return 1;

else

return n * factorial(n – 1);

}

public static double sin(double a) {

if (a == Double.NEGATIVE_INFINITY || !(a < Double.POSITIVE_INFINITY)) {

return Double.NaN;

}

// If you can’t use Math.PI neither,

// you’ll have to create your own PI

final double PI = (22/7);

 

// Fix the domain for a…

// Sine is a periodic function with period = 2*PI

a %= 2 * PI;

 

// Any negative angle can be brought back

// to it’s equivalent positive angle

if (a < 0) {

a = 2 * PI – a;

}

 

// Also sine is an odd function…

// let’s take advantage of it.

int sign = 1;

if (a > PI) {

a -= PI;

sign = -1;

}

 

 // Now a is in range [0, pi].

 

 

// Calculate sin(a)

// Set precision to fit your needs.

 // Note that 171! > Double.MAX_VALUE, so

 // don’t set PRECISION to anything greater

 // than 84 unless you are sure your

 // Factorial.factorial() can handle it

final int PRECISION = 50;

double temp = 0;

for (int i = 0; i <= PRECISION; i++) {

temp += Math.pow(-1, i) * (Math.pow(a, 2 * i + 1) /             factorial(2 * i + 1));

}

return sign * temp;

}

public static void main(String[] args) {

Scanner n = new Scanner(System.in);

System.out.print(“Enter angle: “);

double angle = n.nextDouble();

 

double rad = Math.toRadians(angle);

 

Condition sine = new Condition();

System.out.print(sine.sin(rad) + “\n”);

}

}

 

In order to achieve this presentation we used the following steps:

  1. Research on Power Series of Sine
  2. Understanding how the Power Series works
  3. Write a java code that solves the sine of any angle in degrees
  4. Compile, Debug and run the code
  5. Understanding the java code
0
  • Tweet

About dennis@dennis.me.ke

What you can read next

02. Learning Java in Linux
03. Operators
06. Classes: Store Student Details Using Json Format

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 08. BODMAS IN JAVA

  • 07. JAVA ARRAYS: Store Student Details Using Arrays and List

  • 06. Classes: Store Student Details Using Json Format

  • 02. SPRINT RETROSPECTIVE

  • 01. SCRUM (DEMO)

Categories

  • Databases
  • Java Programming
  • OpenBaraza Framework
  • Scrum

QUICK LINKS

  • Home
  • Our Stories
  • Contact Us
  • Terms and Conditions
  • Our Policy

VISIT US

Open Baraza
Barclays Plaza, 12th Floor.
Loita Street, Nairobi.

Open in Google Maps

GET IN TOUCH

Tel: +254 (20) 2243097 / 2227100
     +254(726)209214, +254(738)819505
Email: info@openbaraza.org

Follow Us

Could not authenticate you.
  • GET SOCIAL
Openbaraza

© All rights reserved | Open Baraza developed by DewCis Solutions.

TOP