Java Programming Exercises and Solutions
Java Programming Exercises and Solutions
2. Write an algorithm to identify if a number is prime or not. The number must be read from the keyboard.
import [Link].*;
public class TestaPrimo {
public static void main(String[] args) {
Scanner N = new Scanner([Link]);
intcontador = 0;
[Link]("Enter the number to be tested:");
intx = [Link]();
for(int i = 2; i <= x; i++)
if(x % i == 0)
counter++;
if(counter == 1)
The tested number is prime
else
[Link]("O número testado não é primo");
}
}
import [Link].*;
public class Fatorial {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter the number to calculate the factorial: ");
intx = [Link]();
intfat = 1;
for(int i = 1; i <= x; i++)
fat = fat * i;
[Link]("The factorial of " + x + " is: " + fat);
}
}
public classFibonnacci {
public static void main(String[] args) {
inta,b,c;
a=1;
b=1;
[Link]("1st Term: " + a);
[Link]("2nd Term: " + b);
for(int i = 2; i < 10; i++) {
c = a + b;
a=b;
b=c;
[Link](i+1 +"º Termo: "+c);
}
}
}
5. Create an array to store grades entered via the keyboard and then calculate the average.
import [Link].*;
public class mediaVetor {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter the number of grades");
doublex [] = new double[[Link]()];
doublesoma = 0;
for(int i = 0; i < [Link]; i++) {
[Link]("Enter the grade: ");
x[i] = [Link]();
soma = soma + x[i];
}
doublemedia = sum/[Link];
[Link]("The average was: " + media);
}
}
import [Link].*;
public class vetorStrings {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter how many names will fill the array");
String x [] = new String [[Link]()];
[Link]("Enter the names that will fill the array");
for(int i = 0; i < [Link]; i++)
x[i]=[Link]();
[Link](x);
for(int i = 0; i < [Link]; i++)
[Link](x[i]);
}
}
import [Link].*;
public class VectorComparison {
public static voidmain(String[] args) {
int[] = new int[3];
int[] = new int[3];
Scanner s = new Scanner([Link]);
[Link]("Vector X");
for(int i = 0; i < [Link]; i++)
x[i] = [Link]();
[Link]("Vector Y");
for(int i = 0; i < [Link]; i++)
y[i]= [Link]();
booleaniguais =true;
intj = [Link] - 1;
for(int i = 0; i < [Link]; i++) {
if(x[i] != y[j])
iguais =false;
j--;
}
if(equals)
The vector X is equal to the inverse of the vector Y!
else
The entered vectors are different!
}
}
8. Write an algorithm to calculate the transpose of a matrix.
9. Create the classes as specified below. Then create another class to create 2 of them.
circumferences and 3 triangles and perform the necessary calculations.
Circumference Triangle
- raio: double - base: double
Circumference (r: double) - altura: double
CalculateArea(): double + Triangulo (b: double, a: double)
+ CalculateDiameter(): double CalculateArea(): double
Gets and Sets Gets and Sets
Class circumference:
public class Circle {
private doubleraio;
publicCircle (double radius) {
[Link]= raio;
}
public doubleCalcularDiametro(){
return 2 * radius;
}
public double CalculateArea() {
[Link] * radius * radius;
}
public doubleCalcularPerimetro(){
return2 * [Link]*radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double newRadius) {
ray = newRay;
}
}
Triangle Class:
public class Triangle {
private double base;
private double height;
10. Crie uma classe chamada Filme e uma classe Oscar para efetuar as operações da primeira.
public classMovie {
privateStringtitulo,genero;
private double nota1, nota2;
private int cont = 0;
import [Link].*;
public class Oscar {
public static void main (String []args) {
Scanner x = new Scanner([Link]);
[Link]("Enter the title and genre of movie A: ");
Movie a = newMovie([Link](), [Link]());
[Link]("Enter the title and genre of movie B: ");
Movie b = new Movie([Link](), [Link]());
[Link]("Enter the ratings for Movie A: ");
for(int i = 1; i <= 3; i++)
[Link]([Link]());
[Link]();
[Link]("Enter the ratings of movie B: ");
for(int i = 1; i <= 3; i++)
[Link]([Link]());
[Link]();
if([Link]() > [Link]())
[Link]("The Oscar goes to " + [Link]());
else
The oscar goes to
}
}
import [Link];
public class BinaryRepresentation {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter the number to be converted to binary: ");
intx = [Link]();
String bin= [Link]( x );
Equivalent in binary is:
}
}
12. Implement an algorithm to print the current system date in the following format: 15th of February
2008 - Friday (5:51:14 PM).
import [Link];
public class Data {
public static voidmain(String[] args) {
String data;
Day of Month de Month Name de Year - Day of Week
hours, minutes and
+"seconds"+"), [Link]());
[Link](data);
}
}
Exercises Class note nº 05:
13. Write an algorithm to print the prime numbers from 2 up to a value entered by the user
public class Primos {
private intn;
public void primeNumbers() {
for(int i = 2; i <= n; i++)
checkingPrime(i);
}
publicPrimos (intn) {
this.n= n;
}
public void checkingPrime(int number) {
inttotal = 0;
for(int i = 2; i <= (number - 1); i++)
if((number % i) == 0)
total++;
if(total == 0)
The number %d is prime.
}
}
import [Link].*;
public class testPrimos {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter the number(s) for the test: ");
intx = [Link]();
Primos object = new Primos(x);
[Link]();
}
}
14. Write an algorithm to print a date entered by the user in another format (use the command
switch).
Entered value: 12/10/1998.
Printed value: October 12, 1998.
import [Link].*;
public classtransformaData {
default:
[Link]("Enter a valid date");
}
if(mes <= 12 )
Transformed data:
}
}
import [Link].*;
public classmediaVetor {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter the number of grades");
doublex [] = new double[[Link]()];
doublesoma = 0;
for(int i = 0; i < [Link]; i++) {
[Link]("Enter the grade: ");
x[i] = [Link]();
soma = soma + x[i];
}
doublemedia = sum/[Link];
[Link]("The average was: " + media);
}
}
16. Write an algorithm to fill an array with names of people (via keyboard), sort it, and display it.
[Link].*;
public class vectorStrings {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
[Link]("Enter how many names will fill the array");
String x[] = new String[[Link]()];
[Link]("Enter the names that will fill the array");
for(int i = 0; i < [Link]; i++)
x[i]=[Link]();
[Link](x);
for(int i = 0; i < [Link]; i++)
[Link](x[i]);
}
}
public classContaEspecialextendsContaCorrente{
private double creditLimit;
publicSpecialAccount(String number, double balance, double limit) {
super(number, balance);
limiteDeCredito= lim;
}
public void debit(double value) {
if(balance+creditLimit>= value)
balance -= value;
else
Limit exceeded
}
}
import [Link].*;
public class BankBrazile {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);
public doublegetBase(){
returnbase;
}
public doublegetAltura(){
returnheight;
}
}
publicTrapezio(doublebase,doublealtura,doublebaseMenor){
super(base, height);
[Link] = baseMenor;
}
public doublecalcularArea(){
return (baseMenor + getBase()) * getAltura() / 2;
}
public double getBaseMenor() {
returnbaseMenor;
}
}
import [Link].*;
public class AreaCalculations {
public static void main(String[] args) {
Scanner a = new Scanner([Link]);