Escolar Documentos
Profissional Documentos
Cultura Documentos
/**
* @(#)QuadraticEquation.java
*/
import java.lang.Math;
public class QuadraticEquation
{
private double a;
private double b;
private double c;
public QuadraticEquation(int varA, int varB, int varC)
{
a = varA;
b = varB;
c = varC;
//double d = (Math.pow(b, 2)) - (4 * a * c);
}
double d = (Math.pow(b, 2)) - (4 * a * c);
public double getSolution1()
{
double solution1 = (-b + (Math.sqrt(Math.pow(b, 2)) - 4 * a * c)) / (2 * a);
if (d>=0)
{
return solution1;
}
else
{
return 0;
}
}
public double getSolution2()
{
double solution2 = (-b - (Math.sqrt(Math.pow(b, 2)) - 4 * a * c)) / (2 * a);
if (d>=0)
{
return solution2;
}
else
{
return 0;
}
}
value = v;
suit = s;
}
public String getNumber()
{
if (value.equals("A"))
{
value = "Ace";
}
else if (value.equals("2"))
{
value = "Two";
}
else if (value.equals("3"))
{
value = "Three";
}
else if (value.equals("4"))
{
value = "Four";
}
else if (value.equals("5"))
{
value = "Five";
}
else if (value.equals("6"))
{
value = "Six";
}
else if (value.equals("7"))
{
value = "Seven";
}
else if (value.equals("8"))
{
value = "Eight";
}
else if (value.equals("9"))
{
value = "Nine";
}
else if (value.equals("10"))
{
value = "Ten";
}
else if (value.equals("J"))
{
value = "Jack";
}
else if (value.equals("Q"))
{
value = "Queen";
}
else if (value.equals("K"))
{
value = "King";
}
else
{
value = "Unknown";
}
return value;
}
public String getSuit()
{
if (suit.equals("D"))
{
suit = "Diamonds";
}
else if (suit.equals("H"))
{
suit = "Hearts";
}
else if (suit.equals("S"))
{
suit = "Spades";
}
else if (suit.equals("C"))
{
suit = "Clubs";
}
else
{
suit = "Unknown";
}
return suit;
}
}
public class CardTester
{
public static void main(String[] args)
{
Card c = new Card("4", "S");
System.out.print(c.getNumber() + " of " + c.getSuit());
}
}
FLOATSORT
/**
* @(#)FloatSort.java
*
* FloatSort application
*
* @author
* @version 1.00 2015/11/12
*/
import java.util.Scanner;
import java.lang.Math;
public class FloatSort
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("First number: ");
double num1 = in.nextDouble();
System.out.println("Second number: ");
double num2 = in.nextDouble();
System.out.println("Third number: ");
double num3 = in.nextDouble();
System.out.println("The inputs in sorted order:");
if (num1 < num2 && num2 < num3)
{
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
}
else if (num2 < num1 && num1 < num3)
{
System.out.println(num2);
System.out.println(num1);
System.out.println(num3);
}
else if (num3 < num1 && num1 < num2)
{
System.out.println(num3);
System.out.println(num1);
System.out.println(num2);
}
else if (num2 < num3 && num3 < num1)
{
System.out.println(num2);
System.out.println(num3);
System.out.println(num1);
}
else if (num3 < num2 && num2 < num1)
{
System.out.println(num3);
System.out.println(num2);
System.out.println(num1);
}
}
}
STRINGSORT
/**
* @(#)StringSort.java
*
* StringSort application
*
* @author
* @version 1.00 2015/11/16
*/
import java.util.Scanner;
public class StringSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("First string: ");
String s1 = in.nextLine();
System.out.print("Second string: ");
String s2 = in.nextLine();
System.out.print("Third string: ");
String s3 = in.nextLine();
if (s1.compareTo(s2) < 0 && s2.compareTo(s3) < 0)
{
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
else if (s2.compareTo(s3) < 0 && s3.compareTo(s1) < 0)
{
System.out.println(s2);
System.out.println(s3);
System.out.println(s1);
}
else if (s3.compareTo(s1) < 0 && s1.compareTo(s2) < 0)
{
System.out.println(s3);
System.out.println(s1);
System.out.println(s2);
}
else if (s1.compareTo(s3) < 0 && s3.compareTo(s2) < 0)
{
System.out.println(s1);
System.out.println(s3);
System.out.println(s2);
}
else if (s2.compareTo(s1) < 0 && s1.compareTo(s3) < 0)
{
System.out.println(s2);
System.out.println(s1);
System.out.println(s3);
}
else if (s3.compareTo(s2) < 0 && s2.compareTo(s1) < 0)
{
System.out.println(s3);
System.out.println(s2);
System.out.println(s1);
}
}
}
GRADE
/**
* @(#)Grade.java
*
* Grade application
*
* @author
* @version 1.00 2015/11/16
*/
public class Grade
{
private String grade;
public String getGrade()
{
if (grade.equals("A") || grade.equals("A+"))
{
grade = "4";
}
else if (grade.equals("A-"))
{
grade = "3.7";
}
else if (grade.equals("B+"))
{
grade = "3.3";
}
else if (grade.equals("B"))
{
grade = "3";
}
else if (grade.equals("B-"))
{
grade = "2.7";
}
else if (grade.equals("C+"))
{
grade = "2.3";
}
else if (grade.equals("C"))
{
grade = "2";
}
else if (grade.equals("C-"))
{
grade = "1.7";
}
else if (grade.equals("D+"))
{
grade = "1.3";
}
else if (grade.equals("D"))
{
grade = "1";
}
else if (grade.equals("D-"))
{
grade = "0.7";
}
else if (grade.equals("F"))
{
grade = "0";
}
else
{
grade = "-1";
}
return grade;
}
}
import java.util.Scanner;
public class GradeTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a letter grade: ");
String letter = in.nextLine();
System.out.print(letter.getGrade());
System.out.print("Numeric value: ");
}
}
BIRTHDAY
import java.util.Scanner;
public class Birthday {
else if (month == 4)
{
if (date >= 1 && date <= 20)
{
System.out.print("Aries, running in circles gets your shoes worn
down on one side");
}
else
{
System.out.print("Taurus, birthdays are like friends- the more you
have, the better");
}
}
else if (month == 5)
{
if (date >=1 && date <= 21)
{
System.out.print("Taurus, birthdays are like friends- the more you
have, the better");
}
else
{
System.out.print("Gemini, if you continually give, you will
continually have");
}
}
else if (month == 6)
{
if (date >= 1 && date <= 21)
{
System.out.print("Gemini, if you continually give, you will
continually have");
}
else
{
System.out.print("Cancer, anything is possible when you own
many rubber stamps");
}
}
else if (month == 7)
{
if (date >= 1 && date <= 22)
{
}
}
else if (month == 11)
{
if (date >= 1 && date <= 22)
{
System.out.print("Scorpio, the most obvious solution is not always
the best");
}
else
{
System.out.print("Sagittarius, sometimes a stranger can bring
great meaning to your life");
}
}
else if (month == 12)
{
if (date >= 1 && date <= 21)
{
System.out.print("Sagittarius, sometimes a stranger can bring
great meaning to your life");
}
else
{
System.out.print("Capricorn, it is better to have a hen tomorrow
than an egg today");
}
}
}
}
SEASON
import java.util.Scanner;
public class Season {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Month: ");
int month = in.nextInt();
System.out.print("Date: ");
int date = in.nextInt();
if (month == 1 || month == 2 || month == 3)
{
System.out.print("Winter");
}
else if (month == 4 || month == 5 || month == 6)
{
System.out.print("Spring");
}
else if (month == 7 || month == 8 || month == 9)
{
System.out.print("Summer");
}
else if (month == 10 || month == 11 || month == 12)
{
System.out.print("Fall");
}
if (month % 3 == 0 && date >= 21)
{
if (month == 1 || month == 2 || month == 3)
{
System.out.print("Spring");
}
else if (month == 4 || month == 5 || month == 6)
{
System.out.print("Summer");
}
else if (month == 7 || month == 8 || month == 9)
{
System.out.print("Fall");
}
else if (month == 10 || month == 11 || month == 12)
{
System.out.print("Winter");
}
}
}
}
ROUNDFLOAT
import java.util.Scanner;
import java.lang.Math;
public class RoundFloat {
public static void main(String[] args) {
double tax = 0;
if (status == SINGLE)
{
if (income > 0 && income <= 8000)
{
tax = 0.10 * income;
}
else if (income > 8000 && income <= 32000)
{
tax = 0.15 * (income - 8000) + 800;
}
else if (income > 32000)
{
tax = 0.25 * (income - 32000) + 4400;
}
}
else if (status == MARRIED)
{
if (income > 0 && income <= 16000)
{
tax = 0.10 * income;
}
else if (income > 16000 && income <= 64000)
{
tax = 0.15 * (income - 16000) + 1600;
}
else if (income > 64000)
{
tax = 0.25 * (income - 64000) + 8800;
}
}
return tax;
}
}
import java.util.Scanner;
public class TaxCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your income: ");
double income = in.nextDouble();
}
GRADE
import java.util.Scanner;
public class GradeTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String num;
System.out.print("Enter a letter grade: ");
String grade = in.nextLine();
if (grade.equals("A") || grade.equals("A+"))
{
num = "4";
}
else if (grade.equals("A-"))
{
num = "3.7";
}
else if (grade.equals("B+"))
{
num = "3.3";
}
else if (grade.equals("B"))
{
num = "3";
}
else if (grade.equals("B-"))
{
num = "2.7";
}
else if (grade.equals("C+"))
{
num = "2.3";
}
else if (grade.equals("C"))
{
num = "2";
}
else if (grade.equals("C-"))
{
num = "1.7";
}
else if (grade.equals("D+"))
{
num = "1.3";
}
else if (grade.equals("D"))
{
num = "1";
}
else if (grade.equals("D-"))
{
num = "0.7";
}
else if (grade.equals("F"))
{
num = "0";
}
else
{
num = "-1";
}
System.out.print("Numeric value: " + num);
}
}
5.19
https://www.daniweb.com/programming/software-development/threads/413864/circle-insidecircle
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.lang.Math;
public class Circle {
private double x;
private double y;
private double radius;
private Color color;
private double px;
private double py;
/**
* Constructs a circle.
* @param xCoord
* @param yCoord
* @param circleRadius
* @param aColor
*/
public Circle(double xCoord, double yCoord, double circleRadius, Color aColor)
{
x = xCoord;
y = yCoord;
radius = circleRadius;
color = aColor;
}
/**
* Draws a circle and a point.
* @param g2
*/
public void draw(Graphics2D g2)
{
g2.setColor(color);
Ellipse2D.Double circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 *
radius);
g2.draw(circle);
}
/**
* Determine if the point is inside the circle.
* @param p
* @return
*/
public boolean isInside(Point2D.Double p)
{
boolean inside;
double d = Math.sqrt((Math.pow(x - px, 2)) + (Math.pow(y - py, 2)));
if (d < radius)
{
inside = true;
}
else
{
inside = false;
}
return inside;
}
}
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
/**
Draws a circle and a point. The point is colored green if it falls
inside the circle, red otherwise.
*/
public class CircleComponent extends JComponent
{
private Circle circle;
private Circle smallCircle;
private double px;
private double py;
public CircleComponent()
{
circle = new Circle(200, 200, 100, Color.BLACK);
Color color;
if(Circle.isInside())
{
color = Color.GREEN;
}
else
{
color = Color.RED;
}
smallCircle = new Circle(px, py, 3, color);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
circle.draw(g2);
smallCircle.draw(g2);
}
}
import javax.swing.JFrame;
/**
This program views a circle and a point.
*/
public class CircleViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CircleComponent component = new CircleComponent();
frame.add(component);
frame.setVisible(true);
}
}
5.20
http://www.dreamincode.net/forums/topic/256681-intersecting-circles/
TAX
/**
* Created by laurenyoon on 11/29/15.
*/
import java.util.Scanner;
public class Taxes {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter your income: ");
double income = input.nextDouble();
System.out.println("Please enter your status: ");
String status = input.nextLine();
double tax;
if (status.equalsIgnoreCase("single")){
if (income >32000){
tax = 4400 + (.25 * (income - 32000));
System.out.println(tax);
}
else if (income >8000){
tax = 800 + (.15 * (income - 8000));
System.out.println(tax);
}
else if (income >0){
tax = .10 * income;
System.out.println(tax);
}
}
else if (status.equalsIgnoreCase("married")){
if (income >64000){
tax = 8800 + (.25 * (income - 64000));
System.out.println(tax);
}
else if (income >16000){
tax = 1600 + (.15 * (income - 16000));
System.out.println(tax);
}
else if (income >0){
tax = .10 * income;
System.out.println(tax);
}
}
}
5.19
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.lang.Math;
public class Circle {
private double x;
private double y;
private double radius;
private Color color;
/**
* Constructs a circle.
* @param xCoord
* @param yCoord
* @param circleRadius
* @param aColor
*/
public Circle(double xCoord, double yCoord, double circleRadius, Color aColor)
{
x = xCoord;
y = yCoord;
radius = circleRadius;
color = aColor;
}
/**
* Draws a circle and a point.
* @param g2
*/
public void draw(Graphics2D g2)
{
g2.setColor(color);
Ellipse2D.Double circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius);
g2.draw(circle);
}
/**
* Determine if the point is inside the circle.
* @param p
* @return
*/
}
}
import javax.swing.JFrame;
import javax.swing.JComponent;
/**
This program views a circle and a point.
*/
public class CircleViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CircleComponent component = new JComponent();
frame.add(component);
frame.setVisible(true);
}
}