0% found this document useful (0 votes)
230 views3 pages

Fatima Ansari's Java Programming Tasks

The document contains code for implementing equations, calculating Fibonacci sequences, finding prime numbers, and generating a basic CV. It includes Java programs to solve these problems by taking user input, performing calculations, and outputting results. The CV program in particular prints personal details, education history, and skills of an individual named xyz.

Uploaded by

Saher ansari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views3 pages

Fatima Ansari's Java Programming Tasks

The document contains code for implementing equations, calculating Fibonacci sequences, finding prime numbers, and generating a basic CV. It includes Java programs to solve these problems by taking user input, performing calculations, and outputting results. The CV program in particular prints personal details, education history, and skills of an individual named xyz.

Uploaded by

Saher ansari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sehar fatima Ansari

12581

OOP task
1. Program the following.

Implement the following equation

3x4sin(180x) + 4x3cos(90x) + x2sin(tan(45)) + 7x + 9cos(90x2 )

Where x may be user defined value.

package equation;

import [Link];

public class Equation {

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

double x=[Link]();

double a=3*[Link](x, 4)*[Link](180*x);

double b=4*[Link](x, 3)*[Link](90*x);

double c=[Link](x, 2)*[Link](1); //tan(45)=1

double d=9*[Link](90*x*x);

double ans=a+b+c+d+7*x;

[Link](ans);

}
}

1. The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence
are 1, 1. Every subsequent value is the sum of the 2 values preceding it. Write a Java
Program that print the nth value of the Fibonacci sequence?

package oop;
public class FibonaciExample {

public static void main(String[] args) {

int maxNumber=10;
int previousNumber=1;
int nextNumber=1;

[Link]("Fibonacci Series of"+maxNumber+"numbers:");


for(int i=1;i<=maxNumber;i++);
{
[Link](previousNumber+"");
int sum=previousNumber+nextNumber;
previousNumber=nextNumber;
nextNumber=sum;

Write a Java program that prompts the user for an integer and then prints out all the
prime numbers up to that Integer?

package oop;
import [Link];

public class Primenumber {

public static void main(String[] args) {


int n;
int p;
Scanner s=new Scanner([Link]);
[Link]("Enter a number:");
n=[Link]();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
[Link](i);

}
}

}
Q: Write a CV program.

public class CVxyz;


{

public static void main(String[] args) {

[Link]("xyz" );
[Link]("xyz@[Link]" );
[Link]( " Personal Data");
[Link]("I am responsible and Deligent student");
[Link](" Date of birth: 00-00-2000");
[Link]("It is not the time to look for excuses");
[Link]( "martial status:Single");
[Link]( " Nationality ");
[Link](" skills");
[Link](" education");
[Link]("English as secondary language");
[Link](" Intermidate: 2018-2020 ,kggdc " );
[Link](" Responsible");
[Link]( "bachelors:2020 present BSCS from iqra");
[Link]("satisfactory commuication skills");

}
}

Common questions

Powered by AI

The CV printing program has a class-level error—its class name declaration syntax is incorrect due to an unneeded semicolon. To resolve syntax issues, remove this semicolon to allow correct class declaration. Ensuring that statements within the method like System.out.println(); are devoid of syntax errors ensures proper output display .

Using hardcoded values, as seen in the examples like maxNumber=10, restricts program scalability and flexibility. It can be addressed by incorporating user input methods for dynamic assignment and using constants or configuration files for managing static values, thus enhancing adaptability and ease of maintenance .

The Scanner class facilitates user interaction by providing methods to read user input from various types such as nextInt and nextDouble. It allows Java programs to be interactive and respond to user-defined inputs, enhancing program functionality and adaptability through the command line interface .

The Prime number Java program can be optimized by implementing the Sieve of Eratosthenes algorithm, which efficiently finds all primes up to a given limit. This method marks non-prime numbers in a boolean array instead of checking each number repeatedly. Additionally, instead of iterating until n, check divisibility only up to the square root of the current number to reduce computations .

The Equation Java program follows basic OOP principles by organizing code into classes and methods, encapsulating functionality within a main method. It could be further improved by separating the mathematical logic into distinct methods or classes to enhance modularity and reusability. Implementing an interface for different mathematical operations can also enhance flexibility .

To enhance output formatting and readability, one can use formatted output with System.out.printf, separating different sections (like personal data, education, skills) using clear headings and line breaks. This improves clarity and structure in the printed information, making it aesthetically pleasing and easier to read .

To include input validation and error handling in the given program, one could implement a try-catch block within the main method. The program should prompt the user for input inside a try block to handle possible exceptions like non-numeric inputs. This can be done using the Scanner class's nextDouble method. If an exception is caught, an error message should be displayed, and the program should re-prompt the user for correct input until a valid number is inserted .

The current implementation of the Fibonacci sequence program contains a syntactical error—an unnecessary semicolon after the loop declaration, leading to incorrect execution. To fix this, remove the semicolon, so the loop properly includes the subsequent code block. Additionally, make the number of terms (maxNumber) user-defined to increase program flexibility by incorporating user input from the Scanner class .

Trigonometric functions like sin and cos in Java use radians, not degrees. The equation implementation must convert angles from degrees to radians using Math.toRadians. A common pitfall is ignoring this conversion, which leads to incorrect results. For example, Math.sin(180) would yield an unexpected result if interpreted directly as 180 degrees .

To expand error handling in the prime number generator program, implement try-catch constructs to catch InputMismatchException for invalid inputs. Prompt the user until correct input is provided. Implement logging to track invalid inputs and use custom exceptions for more specific error messages, enhancing robustness and user interaction .

You might also like