-
Notifications
You must be signed in to change notification settings - Fork 3
Assignment 1
-
Your assignment includes three main classes:
- AveragePrimes.java
- TriangleCheck.java
- CharCount.java
-
Please submit the files no later then 25/03/2020 at 23:59.
-
Algorithm efficiency will not be checked in this assignment.
-
Reminder: Follow the Submission-Instructions.
Write the class
public class AveragePrimes
Which receives an Integer n as an argument and prints out the average of all prime numbers between 2 and n. (including 2 and n)
For example we can use n=5. The prime numbers between 2 and 5 are: 2,3,5
Their average is: (2+3+5)/3 = 3.33
If the input argument is 1 or lower, the program should print “Invalid value”.
Please notice:
- A prime number is a number which divides only by 1 and by itself.
- You can assume the inputs will be numbers.
Examples:
>>java AveragePrimes 5
3.3333333333333335
>>java AveragePrimes 99
42.4
Write the class:
public class TriangleCheck
which receives three decimal numbers as arguments: a,b,c
The program should check two things:
-
Can these three numbers represent the lengths of edges of a triangle.
-
Is one of the angels in this triangle 90 degrees.
If the input has less than 3 values or if one of the values is 0 or less, the program must print “Invalid input”.
If the edges could be a triangle it should print: “triangle!”
If they’re also a right angled triangle it should print “right angled!”.
If they’re not a triangle at all, it should print only “not triangle”.
You can assume the inputs will be numbers.
Examples:
>>java TriangleCheck 3 4 5
triangle!
right angled!
>>java TriangleCheck 3 4 6
triangle!
>>java TriangleCheck 3 4 12
not triangle
Write the class
public class CharCount
which receives a number of words (strings separated by a space) and one char as the last arguments.
The program should print two groups of words: first the words in which the char appears , and then the rest of the words.
Each group of words should be printed in the order that they were recieved.
If the last input is not a char of if there are less then 2 inputs the program should print out: 'Invalid input'
Examples:
>>java CharCount henry is thinking on muffins f
muffins
henry
is
thinking
on
>>java CharCount hello world big lovely day l
hello
world
lovely
big
day
>>java CharCount Hello world ds
Invalid input
- Inside your
mainfunction, you receive the command line arguments as an array of Strings:public static void main(String[] args) - If you have a string str and an array arr :
- Check string length - str.length()
- Check the array size - arr.length
- Get a substring - str.substring(int i, int j) - from the i index (including) to the j index (not incuding) - example: "Hello World".substring(1,7) → “ello W”
- Get element from array by index - arr.index
- Get char from string by index - str.charAt(index)
Download our provided build.xml
and put it under the main directory of your assignment (in the same directory where you have a src subdirectory).
In order to compile and run your project,
open the command prompt, navigate to the assignment directory,
and type the desired ant commands:
>> ant compile
>> ant -Dargs="henry is thinking on muffins f" run3 The last command will run target run3 (corresponding to "Task 3"), and pass in "henry is thinking on muffins f" as command line arguments.