














Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!















area p( pa)(pb)(pc ) 2
a b c p
onde
Enter the measures of triangle X: **3.
5.** Enter the measures of triangle Y: **7.
4.** Triangle X area: 6. Triangle Y area: 7. Larger area: Y
Problema exemplo
package application; import java.util.Locale; import java.util.Scanner;
public class Program {
public static void main(String[] args) { Locale. setDefault(Locale. US); Scanner sc = new Scanner(System. in); double xA, xB, xC, yA, yB, yC; System. out.println("Enter the measures of triangle X: "); xA = sc.nextDouble(); xB = sc.nextDouble(); xC = sc.nextDouble(); System. out.println("Enter the measures of triangle Y: "); yA = sc.nextDouble(); yB = sc.nextDouble(); yC = sc.nextDouble(); double p = (xA + xB + xC) / 2.0; double areaX = Math. sqrt(p * (p - xA) * (p - xB) * (p - xC)); p = (yA + yB + yC) / 2.0; double areaY = Math. sqrt(p * (p - yA) * (p - yB) * (p - yC)); System. out.printf("Triangle X area: %.4f%n", areaX); System. out.printf("Triangle Y area: %.4f%n", areaY); if (areaX > areaY) { System. out.println("Larger area: X"); } else { System. out.println("Larger area: Y"); } sc.close(); } }
Classe
package entities;
public class Triangle {
public double a; public double b; public double c; }
package application;
import java.util.Locale; import java.util.Scanner;
import entities.Triangle;
public class Program {
public static void main(String[] args) { Locale. setDefault(Locale. US); Scanner sc = new Scanner(System. in); Triangle x, y; x = new Triangle(); y = new Triangle(); System. out.println("Enter the measures of triangle X: "); x.a = sc.nextDouble(); x.b = sc.nextDouble(); x.c = sc.nextDouble(); System. out.println("Enter the measures of triangle Y: "); y.a = sc.nextDouble(); y.b = sc.nextDouble(); y.c = sc.nextDouble(); double p = (x.a + x.b + x.c) / 2.0; double areaX = Math. sqrt(p * (p - x.a) * (p - x.b) * (p - x.c)); p = (y.a + y.b + y.c) / 2.0; double areaY = Math. sqrt(p * (p - y.a) * (p - y.b) * (p - y.c)); (...)
Memória
Stack Heap
double areaX, areaY, p; Triangle x, y;
x = new Triangle();
0x100358:
0x
Instanciação
(alocação dinâmica de memória)
Discussão
Memória:
Triangle x, y; x = new Triangle(); y = new Triangle();
package application;
import java.util.Locale; import java.util.Scanner;
import entities.Triangle;
public class Program {
public static void main(String[] args) {
Locale. setDefault(Locale. US); Scanner sc = new Scanner(System. in);
Triangle x, y; x = new Triangle(); y = new Triangle(); System. out.println("Enter the measures of triangle X: "); x.a = sc.nextDouble(); x.b = sc.nextDouble(); x.c = sc.nextDouble(); System. out.println("Enter the measures of triangle Y: "); y.a = sc.nextDouble(); y.b = sc.nextDouble(); y.c = sc.nextDouble();
double areaX = x.area(); double areaY = y.area(); (...)
package entities;
public class Triangle {
public double a; public double b; public double c;
public double area() { double p = (a + b + c) / 2.0; return Math. sqrt(p * (p - a) * (p - b) * (p - c)); } }
package entities;
public class Triangle {
public double a; public double b; public double c;
public double area() { double p = (a + b + c) / 2.0; return Math. sqrt(p * (p - a) * (p - b) * (p - c)); } }
pacote da classe
Nome da classe
O prefixo "public" Atributos da classe indica que o atributo ou método pode ser usado em outros arquivos
Tipo do dado que o método retorna (se o método não retorna nada, usa-se a palavra "void")
Nome do método
Lista de parâmetros do método
Corpo do método
Enter product data: Name: TV Price: 900. Quantity in stock: 10
Product data: TV, $ 900.00, 10 units, Total: $ 9000.
Enter the number of products to be added in stock: 5
Updated data: TV, $ 900.00, 15 units, Total: $ 13500.
Enter the number of products to be removed from stock: 3
Updated data: TV, $ 900.00, 12 units, Total: $ 10800.
Enter product data: Name: TV Price: 900. Quantity in stock: 10
Product data: TV, $ 900.00, 10 units, Total: $ 9000.
Enter the number of products to be added in stock: 5
Updated data: TV, $ 900.00, 15 units, Total: $ 13500.
Enter the number of products to be removed from stock: 3
Updated data: TV, $ 900.00, 12 units, Total: $ 10800.
package entities;
public class Product {
public String name; public double price; public int quantity;
public double totalValueInStock() { return price * quantity; } public void addProducts(int quantity) { this.quantity += quantity; }
public void removeProducts(int quantity) { this.quantity -= quantity; }
public String toString() { return name
package application; import java.util.Locale; import java.util.Scanner; import entities.Product; public class Program { public static void main(String[] args) { Locale. setDefault(Locale. US); Scanner sc = new Scanner(System. in); Product product = new Product(); System. out.println("Enter product data: "); System. out.print("Name: "); product.name = sc.nextLine(); System. out.print("Price: "); product.price = sc.nextDouble(); System. out.print("Quantity in stock: "); product.quantity = sc.nextInt(); System. out.println(); System. out.println("Product data: " + product); System. out.println(); System. out.print("Enter the number of products to be added in stock: "); int quantity = sc.nextInt(); product.addProducts(quantity); System. out.println(); System. out.println("Updated data: " + product); System. out.println(); System. out.print("Enter the number of products to be removed from stock: "); quantity = sc.nextInt(); product.removeProducts(quantity); System. out.println(); System. out.println("Updated data: " + product); sc.close(); } }
Fazer um programa para ler o nome de um aluno e as três notas que ele obteve nos três trimestres do ano (primeiro trimestre vale 30 e o segundo e terceiro valem 35 cada). Ao final, mostrar qual a nota final do aluno no ano. Dizer também se o aluno está aprovado (PASS) ou não (FAILED) e, em caso negativo, quantos pontos faltam para o aluno obter o mínimo para ser aprovado (que é 60% da nota). Você deve criar uma classe Student para resolver este problema.
Entrada: Saída: Alex Green
FINAL GRADE = 90. PASS
Entrada: Saída: Alex Green
FINAL GRADE = 52. FAILED MISSING 8.00 POINTS
membros
=
atributos e métodos
Membros estáticos
Checklist
package application;
import java.util.Locale; import java.util.Scanner;
public class Program {
public static final double PI = 3.14159; public static void main(String[] args) { Locale. setDefault(Locale. US); Scanner sc = new Scanner(System. in); System. out.print("Enter radius: "); double radius = sc.nextDouble(); double c = circumference(radius); double v = volume(radius); System. out.printf("Circumference: %.2f%n", c); System. out.printf("Volume: %.2f%n", v); System. out.printf("PI value: %.2f%n", PI); sc.close(); } **public static double circumference(double radius) { return 2.0 *** PI * radius; } **public static double volume(double radius) { return 4.0 *** PI * radius * radius * radius / 3.0; } }
VERSÃO 1
package util;
public class Calculator {
public final double PI = 3.14159;
public double circumference(double radius) { return 2.0 * PI * radius; }
public double volume(double radius) { return 4.0 * PI * radius * radius * radius / 3.0; } }
Calculator calc = new Calculator();
System. out.print("Enter radius: "); double radius = sc.nextDouble();
double c = calc.circumference(radius);
double v = calc.volume(radius);
System. out.printf("Circumference: %.2f%n", c); System. out.printf("Volume: %.2f%n", v); System. out.printf("PI value: %.2f%n", calc.PI);