Question

In: Computer Science

This homework will allow us to check for understanding and comfort with things you should know:...

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.

  • Language: Java
  • Required parts:
  1. 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)
  2. Write a program that asks the user to enter a number, and then using a loop, outputs all integers, that are smaller than the entered number. (15 points)
  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)
  4. Implement a class called Vehicle, with the following methods (45 points):
    • SetNumberOfPassengers(int)
      • Stores the argument value in an attribute
    • GetNumberOfPassengers()
      • Returns the attribute
    • SetNumberOfWheels(int)
      • Stores the argument value in an attribute
    • GetNumberOfWheels()
      • Returns the attribute
  • Bonus parts:
  1. Write a program that asks the user for a number, and outputs all prime numbers smaller than the number entered (15 points)
  2. Implement a class called Bicycle, which (5 points):
    • Inherits from the class Vehicle you implemented in exercise 4 of the required parts above.
    • Sets the number of passengers to 1.
    • Sets the number of wheels to 1.

Solutions

Expert Solution

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


Related Solutions

Mutation is seen as defective form/functions of living things. But it can also allow us to...
Mutation is seen as defective form/functions of living things. But it can also allow us to learn/experience/explore from it. a. What is genetic mutation? b. What causes mutations? c. Describe some common chromosomal mutation and discuss why are mutations so important to living organisms.
How should marketers go about understanding their customers? What are some of the things they need...
How should marketers go about understanding their customers? What are some of the things they need to do? To create a need, the message content has to show/share the value of the product/service being advertised?
Should China have to devalue their currency to allow US exports to be cheaper to buyers...
Should China have to devalue their currency to allow US exports to be cheaper to buyers in China? The past two presidents have tried negotiations on this and achieved only a small response. How does a devaluation of a country's currency affect its imports and exports?
Should China have to devalue their currency to allow US exports to be cheaper to buyers...
Should China have to devalue their currency to allow US exports to be cheaper to buyers in China? The past two presidents have tried negotiations on this and achieved only a small response. How does a devaluation of a country's currency affect its imports and exports?
in python programming language, please include the proper identation This homework will allow you to demonstrate...
in python programming language, please include the proper identation This homework will allow you to demonstrate understanding and engagement with the following topics: graph representations object oriented programming graph processing,such as finding shortest paths and finding tree traversals Your task is to implement a Graph class. The edges in the graph are undirected, but you must implement an Edge class. In addition, you will have a Graph class, and a Node class. You can choose to implement graphs with any...
What are the things you have to check when choosing stocks to invest on (portfolio) in...
What are the things you have to check when choosing stocks to invest on (portfolio) in terms of Return, Risk, Correlation and Beta. And Why?
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic...
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic and the gates and circuits that embody it. A thorough understanding of boolean logic is extremely useful for almost all programming tasks and necessary to correctly implement complex systems. Additionally, greater familiarity with boolean logic should improve one’s ability to think critically in general as it forms the basis for all human reasoning. Technical Description and Instructions: 1. Consider the logical expression ( !x...
There are several things you need to know before you attempt to select the proper pump....
There are several things you need to know before you attempt to select the proper pump. List at least three points about "How to select the right pump for the job" with brief discussion for each item. Refer to the Pump Application Manual.
Choose a friend or family member that you know well. You should know their family background,...
Choose a friend or family member that you know well. You should know their family background, how they tend to think and make decisions, and how they shop. You must have been on a number of shopping trips with this person. With this knowledge in mind, describe behaviors that demonstrate two out of the 3 major factors influencing consumer behavior (pages 113-123 in your textbook). Each behavior described must represent the influence of a different factor. For example, my dad...
Can you think of any common “real-life” situations in which an understanding of conservation would allow...
Can you think of any common “real-life” situations in which an understanding of conservation would allow a concrete operational child to react differently than a preoperational child? Provide several examples in the space below.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT