outprintlnEnter a number int uni ScannextInt if uni 5 0 Systemoutprintlnuni is

Outprintlnenter a number int uni scannextint if uni 5

This preview shows page 9 - 14 out of 14 pages.

System.out.println("Enter a number : "); int uni = Scan.nextInt(); if (uni % 5 == 0){ System.out.println(uni + " is not a unique number"); }else{ System.out.print(uni + " is a unique number"); } } Print duplicate numbers code: package testreview; import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Duplicate { public static void main(String [] args){ List<Integer> numList = new ArrayList < Integer >(); numList.add( 2); numList.add( 1); numList.add( 5); numList.add( 7);
Image of page 9

Subscribe to view the full document.

numList.add( 9); numList.add( 3); numList.add( 2); numList.add( 7); System.out.println( numList ); List<Integer> duplicateNum = new ArrayList <Integer>(); for (int num : numList){ if(duplicateNum.contains(num)){ System.out.println("Duplicates are:"); System.out.println(num); }else{ duplicateNum.add( num ); } } } } Polymorphism area of shapes code: public class Triangle extends Shape { double len1;// default value 0 double len2; double len3; //constractor public Triangle( double len1, double len2, double len3 ){ this.len1 = len1; this.len2 = len2; this.len3 = len3; System.out.println("Initialize Triangle with len1 "+ len1 + " len2 "+ len2 +
Image of page 10
" len3 "+ len3 ); } double height; public Triangle( double height ){ this.height = height; } @Override public double area() { return 0.5 *( len1+len2+len3 ); } @Override public double parameter() { return len1 + len2 + len3; } } package ploymorphism.example; public class Rectangle extends Shape{ double side1; double side2; public Rectangle( double side1, double side2 ){ this.side1 = side1; this.side2 = side2; System.out.println("Rectangle Initialized with side1: "+ side1 + "side2:"+ side2); } @Override public double area() { // TODO Auto-generated method stub return side1 * side2;
Image of page 11

Subscribe to view the full document.

} @Override public double parameter() { // TODO Auto-generated method stub return 2 * ( side1 + side2 ); } } package ploymorphism.example; public abstract class Shape { public abstract double area(); public abstract double parameter(); } package ploymorphism.example; public class ExecuteShape { public static void main(String[]args){ Triangle tri = new Triangle( 3.0, 4.0, 5.0 ); System.out.println( "Triangle Area : "+ tri.area() ); System.out.println( "Triangle Param: "+tri.parameter() ); Rectangle rec = new Rectangle( 5.0, 20.0 ); System.out.println( "Rectangle Area: "+ rec.area() ); System.out.println( "Rectangle para: "+ rec.parameter() ); } } Stock prices stored and average code: package stocks;
Image of page 12
public class Pandora_App { public static void main(String[]args) { double[] appleStock = { 171.10, 173.0, 170.0, 165.0, 162.0 }; double appleAvgPrice = getAverage( appleStock ); System.out.println( "Average Apple share is: "+appleAvgPrice ); double[] sonyStock = { 46.1, 46.3, 45.0, 46.0, 43.0 }; double sonyAvgPrice = getAverage( sonyStock); System.out.println("Average Sony share is: " + sonyAvgPrice); double[] teslaStock = { 294.4, 276.8, 275.6, 264.7, 270.0}; double teslaAvgPrice = getAverage( teslaStock); System.out.println("Average Tesla share is: " + teslaAvgPrice); if( appleAvgPrice > sonyAvgPrice) { if ( appleAvgPrice > teslaAvgPrice){ System.out.println("The highest average stock to buy is Apple"); }else { System.out.println("The highest average stock to buy is Tesla"); } }else if( sonyAvgPrice > teslaAvgPrice) { System.out.println("The highest average stock to buy is Tesla"); }else { System.out.println("The highest average stock to buy is Sony"); } } public static double getAverage( double [] stock ){ double avg = 0.0; for (int i = 0; i < stock.length; i++) { avg = avg + stock[i]; } return avg / stock.length; }
Image of page 13

Subscribe to view the full document.

}
Image of page 14

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes