Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java 1) Write a complete program that opens the file /usr/abc.txt and that outpu

ID: 3873156 • Letter: J

Question

Java

1) Write a complete program that opens the file /usr/abc.txt and that outputs the smallest value on each line of the file. You may assume that each line contains just 1 or more real values. For example, if the file contained

2.1 5 -9

3

1.4   9.2                  3.8

Your program would output:

-9

3

1.4

(Hint: use more than one Scanner)

2) A class called NumberList represents a sequence of real numbers (doubles). Write the specification (prototype and pre and post conditions, Not The Body) for a member method that adds the data from one NumberList to the end of this NumberList. Of course, no input or output should be involved.

public class NumberList{

private double[] a; private int na; //[0..na-1] holds the data

// ... other stuff ...

// your prototype and pre and post conditions:

3) Student is a class that has just two fields, name and age. It has a no-parameter constructor that always constructs Bob whose age is 22. Assume that the fields are public.

Write a client method (i.e. a method that is not a member of Student) that is passed an array of Student and that returns an independent copy of the array. (Don't try to use clone.)

4) public class Clown extends Object{

   private String name,circus;

   public Clown(String n, String c){name=n;circus=c;}

   public void talk(){

   System.out.println("I'm a clown whose name is "+name);

   }

  }

A happy clown is a clown who says that he is a happy clown whenever he talks (in addition to also telling you his name) and who knows his own age. Write a complete class HappyClown by deriving it from Clown.

Add as little as possible, but provide and label (using a,b,c, and d) the following:

a) A constructor

b) An access method that returns some information about this HappyClown

c) Code that overloads a method or constructor

d) Code that overrides a method or constructor

5) Here is the specification of two instance methods of class Bank. Write the body of the second one (which calls the first one). (Don't write the body of the first one.) Don't use anything else from class Bank. Use try catch so that your sum method doesn't throw any exceptions.

private int assets(String accountId) throws AccountException

//post: the balance held in the account in this Bank whose identifier

//      is accountId is returned, or there is no such account in

//      this Bank and AccountException is thrown

public int sum(String[] list){

//pre: list contains 1 or more strings, all distinct.

//post: returns the sum of the balances of all the accounts in this  

//      Bank whose identifiers are in list. (Elements of list that

//      are not account id's in this Bank are ignored.)

Explanation / Answer

Q-1) Below is the code for Question no 1, which finds the mininum value for each line in file, I have commented the code about where to put file name please change your file name accordingly and test. cheers :) Please ask other questions separately.

import java.io.File;

import java.util.Scanner;

public class MaxEachLine {

public static void main(String[] args) {

try {

Scanner input = new Scanner(System.in);

// provide file name in below

File file = new File("xyz");

input = new Scanner(file);

while (input.hasNextLine()) {

String line = input.nextLine();

String[] values = line.split(" ");

float[] floats = new float[values.length];

int i=0;

for(String value:values)

{

floats[i] = Float.parseFloat(value);

i++;

}

System.out.println(findMax(floats));

  

}

input.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static float findMax(float[] values)

{

float min = Float.MAX_VALUE;

for(float value:values)

{

if(value<min)

min = value;

}

return min;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote