* can pleas write this code to psedocode. I need to explain each code in writen
ID: 3848612 • Letter: #
Question
* can pleas write this code to psedocode. I need to explain each code in writen word pleas?
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.lang.Math;
// First we creat a class tringle
public class Triangle {
// Then we declar two ArryList triple 1 and triple 2
private ArrayList<Integer> triple1;
private ArrayList<Integer> triple2;
// This will Creat scanner to get input for the two triangle , and sava each to an array
// try to constract new tringle instance in the 2 input.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three side lengths of a triangle, separated by spaces: ");
String[] triangle1 = input.nextLine().split(" ");
System.out.println();
System.out.println("Again, enter three side lengths of a triangle, separated by spaces: ");
String[] triangle2 = input.nextLine().split(" ");
System.out.println();
Triangle t = new Triangle(triangle1, triangle2);
//System.out.println(t);
}
// Constrctor chnges arrays into arrylists
public Triangle(String[] triangle1, String[] triangle2) {
triple1 = new ArrayList<Integer>();
triple2 = new ArrayList<Integer>();
for (String side : triangle1) {
if (triple1.size() == 0 ){
triple1.add(Integer.parseInt(side));
}else{
int last = triple1.get(triple1.size()-1);
int newNum= Integer.parseInt(side);
if (newNum >= last){
triple1.add(newNum);
}else{
triple1.add(0, newNum);
}
}
}
for (String side : triangle2) {
triple2.add(Integer.parseInt(side));
}
// Collections.sort(triple1);
// Collections.sort(triple2);
if (getSum(triple1) == getSum(triple2) && triple1.get(2) == triple2.get(2)) {
System.out.println("Yes they are equal");
} else {
System.out.println("No they are not equal");
}
}
// This will implement
public boolean isPythagoreanTriple(ArrayList<Integer> t) {
int a = t.get(0);
int b = t.get(1);
int c = t.get(2);
return (a * a + b * b == c * c);
}
public int getSum(ArrayList<Integer> t) {
return t.get(0) + t.get(1) + t.get(2);
}
}
Explanation / Answer
Psedocode fot the above program:
1. Get side lengths of triangle 1
2. Get side lengths of triangle 2
3. Check if both the triangles are equal
4. Store list of sides of triangle 1 in sorted order in an array, use buuble sort algorithm
5. Store list of sides of triangle 2 in an array
6. If sum of all sides of triangle 1 and 2 is equal and last element of arrays created in #4 and #5 is equal then goto #7 else goto #8
7. Display "Yes the triangles are equal"
8. Display "No they are not equal"
Code with comments:
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.lang.Math;
// First we creat a class tringle
public class Triangle {
// Then we declar two ArryList triple 1 and triple 2
private ArrayList<Integer> triple1;
private ArrayList<Integer> triple2;
// This will Create scanner to get input for the two triangle , and sava each to an array
// try to constract new tringle instance in the 2 input.
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three side lengths of a triangle, separated by spaces: "); // Get sides of triangle 1
String[] triangle1 = input.nextLine().split(" ");
System.out.println();
System.out.println("Again, enter three side lengths of a triangle, separated by spaces: "); // Get sides of triangle 2
String[] triangle2 = input.nextLine().split(" ");
System.out.println();
Triangle t = new Triangle(triangle1, triangle2);
//System.out.println(t);
}
// Constructor changes arrays into integer arraylists
public Triangle(String[] triangle1, String[] triangle2) {
triple1 = new ArrayList<Integer>();
triple2 = new ArrayList<Integer>();
// Sort the sides of triangle 1 and then store it in arraylist
for (String side : triangle1) {
if (triple1.size() == 0 ){
triple1.add(Integer.parseInt(side));
}else{
int last = triple1.get(triple1.size()-1);
int newNum= Integer.parseInt(side);
if (newNum >= last){
triple1.add(newNum);
}else{
triple1.add(0, newNum);
}
}
}
// Save sides of triangle 2 as is in arraylist
for (String side : triangle2) {
triple2.add(Integer.parseInt(side));
}
// Collections.sort(triple1);
// Collections.sort(triple2);
// Check if the triangles are equal using sum of sides of each triangle
if (getSum(triple1) == getSum(triple2) && triple1.get(2) == triple2.get(2)) {
System.out.println("Yes they are equal");
} else {
System.out.println("No they are not equal");
}
}
// This will implement
// This method is not used in above program, but it is used to check if the triangle's sides are Pythagoream triplet
public boolean isPythagoreanTriple(ArrayList<Integer> t) {
int a = t.get(0);
int b = t.get(1);
int c = t.get(2);
return (a * a + b * b == c * c);
}
// Function returns the sum of elements of an arraylist
public int getSum(ArrayList<Integer> t) {
return t.get(0) + t.get(1) + t.get(2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.