In: Computer Science
This homework will allow us to check for understanding and comfort with things you should know: Input/output, variables, expressions, conditionals, loops, arrays, functions, and classes.
Write a program that asks the user for a number, then asks for a second number, and finally prints if the first is smaller than the second, greater than the second, or equal to the second. (10 points)
// CompareTwoNumbers.java
import java.util.Scanner;
public class CompareTwoNumbers {
public static void main(String[] args) {
int first,second;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter First Number:");
first=sc.nextInt();
System.out.print("Enter Second Number:");
second=sc.nextInt();
if(first<second)
{
System.out.println(first+" is greater than "+second);
}
else if(first>second)
{
System.out.println(first+" is less than "+second);
}
else
{
System.out.println(first+" is equal to "+second);
}
}
}
======================================
output:
Enter First Number:34
Enter Second Number:56
34 is greater than 56
===========================================
3)
Write a program that creates an array of 10 random numbers. After the array is created, a function receives the array, and returns the index of the highest element. (30 points)
// RandomOf10Nos.java
public class RandomOf10Nos {
   public static void main(String[] args) {
      
   int nos[]=new int[10];
   for(int i=0;i<nos.length;i++)
   {
      
nos[i]=(int)(Math.random()*(100))+1;
   }
   int maxIndex=findLargestElementIndex(nos);
   System.out.println("Largest Element index is
:"+maxIndex);
   }
   private static int findLargestElementIndex(int[]
nos) {
       int maxIndex=0;
       int max=nos[0];
       for(int
i=0;i<nos.length;i++)
       {
          
if(max<nos[i])
           {
          
    max=nos[i];
          
    maxIndex=i;
           }
       }
       return maxIndex;
   }
}
=======================================
Output:
Largest Element index is :7
=========================================
4)
// Vehicle.java
public class Vehicle {
   private int numberOfPassengers;
   private int numberOfWheels;
   /**
   * @param numberOfPassengers
   * @param numberOfWheels
   */
   public Vehicle(int numberOfPassengers, int
numberOfWheels) {
       this.numberOfPassengers =
numberOfPassengers;
       this.numberOfWheels =
numberOfWheels;
   }
   /**
   * @return the numberOfPassengers
   */
   public int getNumberOfPassengers() {
       return numberOfPassengers;
   }
   /**
   * @param numberOfPassengers
   * the numberOfPassengers to set
   */
   public void setNumberOfPassengers(int
numberOfPassengers) {
       this.numberOfPassengers =
numberOfPassengers;
   }
   /**
   * @return the numberOfWheels
   */
   public int getNumberOfWheels() {
       return numberOfWheels;
   }
   /**
   * @param numberOfWheels
   * the numberOfWheels to set
   */
   public void setNumberOfWheels(int numberOfWheels)
{
       this.numberOfWheels =
numberOfWheels;
   }
}
=========================================
// Bicycle.java
public class Bicycle extends Vehicle {
public Bicycle() {
   super(1,1);
   }
}
=====================================
// Test.java
public class Test {
   public static void main(String[] args) {
       Bicycle b=new Bicycle();
       System.out.println("No of
Passengers :"+b.getNumberOfPassengers());
       System.out.println("No of Wheels
:"+b.getNumberOfWheels());
      
}
}
====================================
Output:
No of Passengers :1
No of Wheels :1
==================================
Write a program that asks the user for a number, and outputs all prime numbers smaller than the number entered (15 points)
// PrimeNumbers.java
import java.util.Scanner;
public class PrimeNumbers {
   public static void main(String[] args) {
int num;
       /*
       * Creating an Scanner class object
which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new
Scanner(System.in);
       //Getting the input entered by
the user
System.out.print("Enter a number :");
num=sc.nextInt();
for(int i=2;i<num;i++)
{
   if(isPrime(i))
   {
       System.out.print(i+" ");
   }
  
}
System.out.println();
}
//This method will check whether the number is prime or
not
private static boolean isPrime(int n) {
// If the user entered number is '2' return true
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
}
=========================================
output:
Enter a number :50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
=======================================Thank You