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

Matlab Code Function Name: albumArt Inputs: 1. ( char ) The name of a text file

ID: 3723267 • Letter: M

Question

Matlab Code

Function Name: albumArt

Inputs:

1. ( char ) The name of a text file containing ASCII album art Outputs:

None

File Outputs:

1. A text file containing the review of the album art

Banned: fileread() and textscan()

Background:

You have just been hired to rate all of the art in a old shop that still sells this ancient technology: "CDs". You want to get out of this musty old place as quickly as possible, so you decide to use your handy MATLAB knowledge to rate all of the albums for you so you can go grab a tall, nonfat latte with caramel drizzle from Starbucks.

Function Description:

Given a text file, you must read the average value of all the characters in the image using the table below:

Character

' @ '

' % '

' ? '

' ! '

' * '

' ~ '

' . '

' '

Darkness

1   

2

3

4

5

6

7

8

Once you have found the average value of the all the characters in the text file based on darkness, you must determine a descriptive statement. If the average darkness is greater than 5, the descriptive statement is 'Very light cover, probably good music for kids!' , if the average darkness is greater than 3, the descriptive statement is 'Cover of medium darkness, seems like it could be a bit too edgy...' , and for all other possibilities the descriptive statement is 'Cover is very dark, looks like something quite inappropriate for children.' . Then, print out a text file named '<name of the file>_review.txt' with the following form:

Notes:

All your output files should be 3 lines long.

Your darkness score should be rounded to 2 decimal places.

Character

' @ '

' % '

' ? '

' ! '

' * '

' ~ '

' . '

' '

Darkness

1   

2

3

4

5

6

7

8

Explanation / Answer

package codeTesting;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

public class MatCode {

private static double getAvg(double val) {

val = val * 100;

val = Math.round(val);

val = val / 100;

return val;

}

private static void PrintFile(File file) throws IOException {

Map<Character, Integer> map = new HashMap<Character, Integer>();

try {

FileInputStream fis = new FileInputStream(file);

char current;

while (fis.available() > 0) {

current = (char) fis.read();

//System.out.print(current);

if (map.containsKey(current)) {

int value = map.get(current);

value++;

map.put(current, value);

} else {

map.put(current, 1);

}

}

} catch (IOException e) {

e.printStackTrace();

}

Set<Entry<Character, Integer>> entries = map.entrySet();

double totalDarkness = 0, totalChars = 0;

for (Map.Entry<Character, Integer> entry : entries) {

Character key = entry.getKey();

Integer value = entry.getValue();

//System.out.printf("key: %s, value: %d %n", key, value);

totalChars += value;

if (key == '@') {

totalDarkness += value * 1;

} else if (key == '%') {

totalDarkness += value * 2;

} else if (key == '?') {

totalDarkness += value * 3;

} else if (key == '!') {

totalDarkness += value * 4;

} else if (key == '*') {

totalDarkness += value * 5;

} else if (key == '~') {

totalDarkness += value * 6;

} else if (key == '.') {

totalDarkness += value * 7;

} else if (key == ' ') {

totalDarkness += value * 8;

}

}

double avgDarkness = getAvg(totalDarkness / totalChars);

String descStatement = "";

if (avgDarkness > 5) {

descStatement = "Very light cover, probably good music for kids!";

} else if (avgDarkness <= 5 && avgDarkness > 3) {

descStatement = "Cover of medium darkness, seems like it could be a bit too edgy...";

} else {

descStatement = "Cover is very dark, looks like something quite inappropriate for children.";

}

String fName = file.getName();

String form="Review of " + fName.substring(0, fName.lastIndexOf(".")) + "'s album art:"

+" "+"The cover has a darkness score of " + avgDarkness+" "

+ descStatement;

String outputFilename=fName.substring(0, fName.lastIndexOf(".")) + "_review.txt";

log(form,outputFilename);

}

public static void log(String message,String fName) throws IOException {

PrintWriter out = new PrintWriter(new FileWriter(fName, true), true);

out.write(message);

out.close();

}

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

File file = new File(args[i]);

if (!file.exists()) {

System.out.println(args[i] + " does not exist.");

return;

}

if (!(file.isFile() && file.canRead())) {

System.out.println(file.getName() + " cannot be read from.");

return;

}

String fName = file.getName();

//System.out.println("---" + fName.substring(0, fName.lastIndexOf(".")) + "_review.txt---");

try {

PrintFile(file);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}