Violet_82 89 Posting Whiz in Training

Ah wait, you mean something like this then:

var jsonString = '[' +
        '{"text":"Lorem ipsum dolor sit amet, consectetur  0", "firstName":"Jack0", "surname":"Dee0", "moreInfo":"This is extra information"},' +
        '{"text":"Lorem ipsum dolor sit amet, consectetur  1", "firstName":"Jack1", "surname":"Dee1", "moreInfo":"This is extra information"}' +
    ']';

But I thought that the specification said that a json string has to start with a { and not with a [, in fact the first version of the string starts with a curly brace.
So if I start with a [, is there any implication? what I mean is that this json string will be sent to the application from somewhere not yet specified, I'll parse it, store the results in a variable, loop through the literal objects and create an object for each line. Then I'll do some data manipulation, change the properties of the object, stringify the back into a json string and send it back

Violet_82 89 Posting Whiz in Training

HI guys,
I seem to be having an issue declaring a json string.
The original string I have is this:

var jsonString = '{ "details" : [' +
    '{"text":"Lorem ipsum dolor sit amet, consectetur  0", "firstName":"Jack0", "surname":"Dee0", "moreInfo":"This is extra information"},' +
    '{"text":"Lorem ipsum dolor sit amet, consectetur  1", "firstName":"Jack1", "surname":"Dee1", "moreInfo":"This is extra information"},' +
    ']}'; 

and if I parse it

jsonStringParsed = JSON.parse(jsonString);
console.log(jsonStringParsed);

I get what I expected in the console, two objects:

details:Array[2]
        0:Object
            firstName:"Jack0"
            moreInfo:"This is extra information"
            surname:"Dee0"
            text:"Lorem ipsum dolor sit amet, consectetur  0"

        1:Object
            firstName:"Jack1"
            moreInfo:"This is extra information"
            surname:"Dee1"
            text:"Lorem ipsum dolor sit amet, consectetur  1"

The problem is that after I parse it and loop through the objects in jsonStringParsed I don't want to have to use the syntax jsonString.details.xxxx, but I want direct access to the object, something like this instead jsonString.xxxx(the reason doesn't matter too much at this stage as this is part of a much larger application) so I decided to change the declaration of the json string to this:

var jsonString = 
    '{"text":"Lorem ipsum dolor sit amet, consectetur  0", "firstName":"Jack0", "surname":"Dee0", "moreInfo":"This is extra information"},' +
    '{"text":"Lorem ipsum dolor sit amet, consectetur  1", "firstName":"Jack1", "surname":"Dee1", "moreInfo":"This is extra information"}';

But this doesn't work, and I don't quite understand why as nowehere - not that I could find it at least - said that a json string can't be declared like that, quite the contrary in fact. Does anybody have any idea why …

Violet_82 89 Posting Whiz in Training

@Geoff_3, in addition to the above, I also thought about an issue. The above formula, I presume, will work when I move a weight around the scale or add one, which is great, but there is also one more thing. As I mentioned before the limits are initiallly set before the animation takes place, when the angle is 0, but there might be already a few weights on the scale, like, preset weights if you like, which means that the limits needs to be updated.
So, if a weight is dropped on the scale - either because it's created or because it's moved - then I can use the data from that weight to work out the limits (using the formula because I can find out how far the weight is from the pivot) but when there are, say, 6 weights on it to start with and I need to work out the limits, as the formula asks for the distance between a specific weight from the pivot, it won't work as there are 6 weights on it ? Do you know what I mean?

Violet_82 89 Posting Whiz in Training

thanks for the kind words @JamesCherrill :-)
@ Geoff_3, thanks for the post and the formula. Let me see if I understood correctly what you're saying. With your equation in mind, would my limits be:

LL1 += cos(a) * x
LL2 += cos(a) * x
LR1 -= cos(a) * x  
LR2 -= cos(a) * x

Also, I recalculate the limts after the rotation so x would be the new distance between the weight and the pivot after the rotation has taken place. Correct?
thanks

Violet_82 89 Posting Whiz in Training

Hi guys I wonder if you can help me with this as it's driving me absolutely crazy.
So, I have a div, in the first screenshot which looks pretty much like a scale and can be rotated right up to 45 degrees and left up to -45 degrees
arm_0.jpg

The rotation occurs when something (a div) is dropped on the arm and based on the properties of this div the rotation is worked out and takes place. Of course there can be lots of divs in the scale, but here for the sake of simplicity we'll see only one.
Now, I set some limits as you can see, because when something is dropped on this scale I need to know in which section (0,1,2) of which side of the arm, the weight is.

LL1 = $(".arm").offset().left + this.unitSize;//where unitSize is the width of section 0,1,2 etc and arm is the actual horizontal div
LL2 = this.LL1 + this.unitSize;
LR1 = $(".arm").offset().left + ($(".arm").width() / 2 + $(".pivot").width() / 2 ) + this.unitSize;//I need to make sure I exclude the pivot
LR2 = this.LR1 + this.unitSize;

So, these limits, the first time, are worked out at the beginning before the rotation takes place as part of the initialization, so when the angle is 0.
When the arm rotates though I need to recalculate them, and that's where I'm having loads of problems because, after the rotation, the coordinates of the limits …

Violet_82 89 Posting Whiz in Training

Ah sorry, one more thing: even if I manage to do that the actual tick images will not be in the same position for both sides of the div. If you look at this screenshot

spans2.jpg

you will notice that the the tick image is on the right of the span (position 100% 0) which is OK for the left-hand side of the rect div, but once they are added on the right, the tick image should be displayed on the left of the tick span, and I don't see how I could achieve that.
I played around a bit more and I managed to find a solution, even if admittedly I'm not at all happy with the implementation. I've created two for loops, one for the left and one for the right side of rect. Here is the code:

 $(document).ready(function(){
            var leftSide = (($(".rect").width() / 2) - ($(".mid").width() / 2));
            var rightSide = (($(".rect").width() / 2) + ($(".mid").width() / 2));

            var units = leftSide / 3;
            var tickImg = "<span class='tick container_" + i + "'></span>";
            $(".mid").css("left",leftSide);
            for(var i = 0; i < 3; i++ ){
                $(".rect").prepend("<span class='tick left container_" + i + "'></span>").find(".tick").width(units);

            }

            for(var i = 0; i < 3; i++ ){
                // $("<span class='tick container_" + i + "'></span>").insertAfter(".mid").find(".tick").width(units);
                $(".mid").after("<span class='tick right container_" + i + "'></span>").next(".tick").width(units);
            }

The square div is positioned absolutely so it's out of the way and the two sides of rect are specular. Here …

Violet_82 89 Posting Whiz in Training

Why not build your 'ruler' in one step and use the magic of Z-Axis to place the square wherever you'd like over top of it?

Because the width of the square counts as we can see from the last screenshot. If I loop 6 times and create 6 spans, somehow I have to be able to add the width of the square after 3 spans if you know what I mean otherwise the 4th span will start in the middle - or thereabout - of the square. So in other words I should be able to do: span1, span2, span3, width of the square, span 4, span5, span6

Violet_82 89 Posting Whiz in Training

thanks guys, let me address your answers one by one and then I'll go throught what else I've done that might help
@Chris_26
I tried your first solution but it doesn't work: here is the code, with some additions to tailor to my needs:

$(document).ready(function(){
    var leftSide = (($(".rect").width() / 2) - ($(".mid").width() / 2));
    var rightSide = (($(".rect").width() / 2) + ($(".mid").width() / 2));

    var units = leftSide / 3;
    var tickImg = "<span class='tick'></span>";
    var tally = 0;
    for(var i = 0; i < 4; i++ ){
    console.log("for the left " + (leftSide - (tally*units)));

    $(".rect").prepend(tickImg).find(".tick").css({top:0,left:"+=" + (leftSide - (tally*units)) + "px"});

    tally += 1;
}

My console log prints the right values, starting from 410 to 0, but when I execute the script the positions are not right as we have 0, 136.666, 410, 820
Here is a screenshot:
spans.jpg
Also I have to add that the segments to add are 3 and not 4 as previously mentioned, sorry, 3 on the left and 3 on the right of the div, so looping 3 times now

Out of curiosity, what's stopping you from just using a pseudo-element, on both sides, each with tiled background?

Admittedly I didn't think about that, but also I've never used them either. I had a quick look at them, thought, but how would they help me? Can you elaborate a bit more?

@Lusiphur, I don't ignore anybody unless spam.

1) …

Violet_82 89 Posting Whiz in Training

hi guys, I came across something rather interesting today.
Basically at the moment I'm trying to add multiple spans with a background image to a horizontal div so that the final product looks something like a large ruler ( I use an image with a little black tick to simulate the rulers ticks, something like this):
half_ruler1.jpg

This horizontal div is divided in two halves and I need to add one rule on each side, (if I do one side I reckon the other would be easy to do although they are specular).
Let's just focus on the left side only, for simplicity:
So the thicks are inside a span and the actual tick is an image as I mentioned: so I add these spans through a for loop, looping 4 times (because I need 4 ticks) and each time I create and insert the span in the DOM.
So far so good.
What I seem to be having huge problems with is to assign each span a different css left value, decremental in this case: my calculations says that I need to add the first tick at 410px (which is just before the square), the second at 410 - 136.666 and so on, decrementing always 136.666 all the way to zero, so that we start from right and we go all the way to the left (0).

Let's look at some code, that will hopefully be clearer.

Violet_82 89 Posting Whiz in Training

Thanks for that. After a bit of thinking I realized that I can probably use Jquery to do the whole thing. I played around with D3 a bit and what you said is right, although I managed to knock off a simple scale it's more for graphs and things like that. I checked out the matter-js library too: it's quite nice what it can do, but on reflection I may not need that either. Essentially what I need is:
-drawing rectangles on a panel: easily dable in Jquery
-drag and drop functionalities: can be done in Jquery
-divide the above rectangle in two halves (let's call them arms) and have a rule on each half, going from 1-6. This rule has to be functional and because the idea is that users will be able to drag and drop weights or balls or whatever else on these arms (left and right) at specific points (like dropping something at point 2 and/or 3 etc) I need to know at which point the balls are attached to determine which side the arm needs to swing and how much
What do you reckon?

Violet_82 89 Posting Whiz in Training

thanks, yes yesterday I wasn't sure either to be honest, that's why I've been so general. Now I know a bit more.
Basically, it's an animated and interactive application. It consists of an interactive and animated scale and depending on what you add on this scale it will lean on one side or the other, and it needs to do that with an animation.
So, to summarize: the scale will have two arms, one on the right and one on the left. Each arm will have some kind of scale, going from, say, 1 to 5 where 1 is the 'lightest' and 5 the 'heaviest'. Users can add things to the arms (I haven't figured out how I will visualize this action, perhaps they can add balls of different sizes with a number to indicate weight to one arm or the other of the scale or something along these lines) and visually we need to see which way the scale leans, left or right. Of course you can remove the balls or even slide them along the arm of the scale, and that will affect the side the scale is leaning to, again all this needs to be animated.
I'm playing around with D3 this morning, just to get a feel for what it can do.
I reckon it might be doable with Java FX too but to be honest I reckon I'd feel more at home with D3, if it is capable of doing such …

Violet_82 89 Posting Whiz in Training

HI guys, I wonder if you can help me at all. I'm going to be involved in a "data visualization" project, meaning that I have to find a good way to visualize some data. What do you mean I hear you say? Well, we have some data and we have to make it look attractive (this data is a little interactive in that users will be able to interact with it, like drag and drop stuff etc).
I have absolutely zero experience with visualization and I'm looking around the net for ideas. So, I was just wondering if any of you has any experience in prettify data , visualizing it and presenting it to users. I have no idea what I can use to develop that, javascript for example, or prebuilt software or whatever else. I've found something interesting today. a library called Processing.js which seems promising but I have no idea, like it's not too clear at what level it supports users interaction and how easy it is (I quickly played around with it for about 10-15 minutes and it seems all right) hence my post.
Does anybody have any nice suggestion or even share his/her experience?
thanks

Violet_82 89 Posting Whiz in Training

I agree and disagree with diafol.
Don't try convert your website to support mobile/tablets etc, I'd be very surprised if you can actually do that, but regardles, as diafol said, it will be incredibly painful and you will reach the point where you think you're wasting your time. You will.
In terms of approaches, I disagree with starting from mobile first - although people probably use more mobile devices than desktop and this surely is an argument in favour of starting with mobile first - but remember that it's actually easier to hide not needed elements than it is to create them ad hoc, meaning, if you start from desktop and go all the way to mobile (via tablet that is) all you've got to do is to hide what you don't want mobile users to see with, literally, just a line of jquery.
Another thing to bear in mind is backward compatibility: how far are you going with browsers? Some old versions of IE don't support media queries so you might end up displaying a mobile site on a desktop (if your IE version is old enough). Not critical, but worth bearing it in mind.
In my personal experience it's easy enough to create well defined break points to fit most devices. It is kind of difficult to go through the all things in a post, best to look at some examples. I've done an old website some time ago where I store useful code, …

Violet_82 89 Posting Whiz in Training

Hi guys, I will soon have a licence to run webstorm on one machine (at work) but I need to think of some other free IDE which supports typescript ideally natively on a different laptop running windows and linux (at home).
I had a look around of course, but as I don't have any experience with any of them I thought I'd ask here.
For windows I know that there is visual studio core, although I'm not sure that's free.
I have eclipse installed already, both on linux and on windows but I've only used it for java, never for javascript (read angularjs) and typescript. In a way it would kind of make sense to use eclipse but I've heard it might be pretty slow, which might be why at work we use webstorm, what do you guys think?
I've also heard of atom, has anybody used it? Is it linux compatible too?

Any idea/suggestion is of course much appreciated

Violet_82 89 Posting Whiz in Training

Hi thanks, I think I feel better if I return false or true.
So, thinking of the actual function would it be something like this:

public boolean validateOutput(String name, String surname, String age, String sex, String role, String id, String streetNumber, String streetName, String postcode) {
if(fields are empty){
        generateError();
        return false;
    }
else{
    if(not number){
            generateError();
            return false;
        }
    else{
        return true;
    }
}

If the above is OK, I'm thinking that perhaps I could group all the textFields in an array or something so that I can check them all for empty values pretty quickly, what do you reckon?

Violet_82 89 Posting Whiz in Training

Right, I've had a change of heart and refactored the whole thing, because, well, I suppose with time you tend to change your mind :-). SO here are the changes I've made.
1)moved the application into vaadin, as I needed to run it into the browser in a web form (and besides, as much as I don't like it, it's the framework used in the office, so the more I work with it the better, that's at least until they decide to go for another framweork.)
2)removed the client class, person class and some more stuff, so that now I have a webform which users can fill in with the employee's detail and ideally submit it (I'm thinking to submit it to a text file but I haven't implemented that functionality as yet, for now it is saved onto a List), and of course I got stuck with something, but let's look at the code first. So far I have:
1)the Address.java

package employee;

public class Address {
    private int streetNumber;
    private String streetName;
    private String postcode;

    public Address(int streetNumber, String streetName, String postcode){
        this.streetNumber = streetNumber;
        this.streetName = streetName;
        this.postcode = postcode;
    }

    //getters
    public int getStreetNumber(){
        return streetNumber;        
    }
    public String getStreetName(){
        return streetName;
    }
    public String getPostcode(){
        return postcode;
    }
    @Override
    public String toString(){
        return String.format("\nStreet number: %d, \nStreet name: %s, \nPostcode: %s", getStreetNumber(), getStreetName(), getPostcode());
    }
}

2)Employee.java

package employee;

public class Employee {
    private String name;
    private String surname;
    private Address address; …
Violet_82 89 Posting Whiz in Training

Hi sorry I left this for so long, but other things got in the way...Anyway, in this case I would just want to print the details out, so a method like this should suffice I reckon:

printPerson(person){
//  print person (employee or client)
}

So at the moment I'm creating employees and clients manually like so:

Address address = new Address(34, "Flinch Street","KT25AG");
        Address address1 = new Address(44, "London Road", "SW17FG");
        Address address2 = new Address(356, "Dean Street", "SW94TG");
        Employee employee1 = new Employee("John","Smith", 39, 1250.50, 3456, "Permanent", address);
        Employee employee2 = new Employee("Mike","Donovan", 59, 1950.50, 1456, "Contractor", address1);
        Client client1 = new Client("Jack", "Milt", 40, address2);

and I would like to put them in a List - to start with. Do I actually need two Lists, one for Client and one for Employee do you reckon? Or, thinking about it, Client and Employee both inherit from Person, so I might get away by creating a list of Person and store Client and Employee in it...

Violet_82 89 Posting Whiz in Training

yes I think that would do as well, sorry for the delay

Violet_82 89 Posting Whiz in Training

cool thanks guys, I will do that

Violet_82 89 Posting Whiz in Training

Hi guys, I'm a git first time user, and I followed this guide to set it up and get it up and running https://git-scm.com/documentation.
So I'm now using git from the console (at the moment I'm logged in windows, so let it be the windows console). My question is - because I couldn't find this on the guide - say I'm logged in from another machine and I want to checkout my project, keep working on it and then commit the changes, is it possible? In other words, how do I access my existing repository from another machine?

Violet_82 89 Posting Whiz in Training

Nevermind, I think I figured that out eventually...

dictionary = {"expenses":
    [
        {"Syn":"expenditures"}
    ],
    "paper":
    [
        {"Syn":"study"}
    ],
    "javascript":
    [
        {"Syn":"scripting"}
    ],
    "creating":
    [
        {"Syn":"generating"}
    ]
}
Violet_82 89 Posting Whiz in Training

Hi guys, I just don't seem to get this right at the moment...say I want to create an array of objects, I'd do it this way of course:

arrayOfWords[
    {
        "SearchTerm":"mySearchTerm1",
        "Syn":"synonym1",
        "Syn":"synonym2",
        "Syn":"synonym3"
    },
    {
        "SearchTerm":"mySearchTerm2",
        "Syn":"synonym1",
        "Syn":"synonym2",
        "Syn":"synonym3"
    }   
]

That's all OK but I'm now in a situation where I need to loop through that object, check that the SearchTerm matches the value of another variable declared and set elsewhere and if so I need to loop through the synonyms, so this structure isn't too good because all the elements (SearchTerms and Syn) are on the "same" level: I need to know how many synonyms I have in the array so that I can easily loop through them, so I thought that it might be a good idea to have the synonyms as objects themselves, but I'm having some problems with the syntax, how would I define an array of objects of objects? WOuld it be something like this:

arrayOfWords[
    {
        "SearchTerm":"mySearchTerm1"
            {
                "Syn":"synonym1",
                "Syn":"synonym2",
                "Syn":"synonym3"
            },
        "SearchTerm":"mySearchTerm2",
            {
                "Syn":"synonym1",
                "Syn":"synonym2",
                "Syn":"synonym3"
            }
    }
]

So that each SearchTerm has a number of synonyms?
thanks

Violet_82 89 Posting Whiz in Training

Nothing, the above is internal to Adobe acrobat so it literally is those lines of code pasted in this peculiar adobe console: it resturns the results in a nice window - I presume under the hood it's a json objec of some kind but from there you don't have access to the code. I've looked a bit into pdf.js, https://github.com/mozilla/pdf.js as that seems to be the way forward, although installing and getting it up and running is proving rather challenging as not everything works the way it should, especially running gulp. I failed in the office, so i'll try on my laptop at home this evening hopefully, a clean install of nodejs and everything else, hoping to get some help from their irc channel
Has anybody used pdf.js?

Violet_82 89 Posting Whiz in Training

Hi guys, I'm looking into the possibility of writing some javascript (or using an API of course) to be able to search a collection of PDFs and return the results in a Json format for processing (mainly displaying them back to the user). I've played around a little bit with something called the adobe acrobat console, which essentially it's a console running inside adobe acrobat reader which allows you to run some javascript.
The following simple snippet runs in that console and returns a list of results in all the PDFs selected:

search.matchCase = false;
search.wordMatching = "MatchAnyWord";
search.bookmarks = true;
search.query("will","Folder","/C/Users/xxx/Desktop/PDFs");

I've basically created a folder called PDFs where I stored 2 PDFs and then I run this search which returns all the results in a separate window. That's great, but I need to be able to "export" this functionality and pack it up in a script. Has anybody got any idea? Or even better, has anybody done this before?
cheers

Violet_82 89 Posting Whiz in Training

Yes, spot on, I wasn't assigning address to anything, no wonder it didn't work : -)! Below is the full code so far. I think that I'd like to add my employees and clients objects in a List, as we discussed earlier, and perhaps sort them in alphabetical order, somthing like that, unless there is anything else that I need to change before doing so:
Person.java

public abstract class Person
{
    private String name;
    private String surname;
    private int age;
    private Address address;
    public Person(String name, String surname, int age, Address address){   
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.address = address;

    }
    public void setName(String name){
        this.name = name;
    }
    public void setSurname(String surname){
        this.surname = surname;
    }
    public void setAge(int age){
        this.age = age;
    }

    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public int getAge(){
        return age;
    }
    @Override
    public String toString(){       
        return String.format("\nName: %s,\nsurname: %s,\nage: %d, \naddress details: %s ", getName(), getSurname(), getAge(), address.toString());
    }
}

Employee.java

public class Employee extends Person
{
    private double salary;
    private Identification identification;
    //private Address address;
    public Employee(String name, String surname, int age, double salary, int id, String role, Address address){
    //public Employee(String name, String surname, int age, double salary, int id, String role, int streetNumber, String streetName, String postcode){
        super(name,surname,age, address);
        //System.out.print("I'm an Employee. ");
        this.salary = salary;
        identification = new Identification(id, role);
        //address = new Address(streetNumber, streetName, postcode);
    }
    public double getSalary(){
        return salary;
    }
    public void setSalary(double salary){
        this.salary = …
Violet_82 89 Posting Whiz in Training

OK, so not sure I've done it correctly. Here is the new logic followed by the code.
TestPerson.java - assuming that everybody has an address as said before - creates the Employees, Clients and Address objects, here the class in full:

public class TestPerson
{

    public static void main(String[] args)
    {       
        Address address = new Address(34, "Flinch Street","KT25AG");
        Address address1 = new Address(44, "London Road", "SW17FG");
        Address address2 = new Address(356, "Dean Street", "SW94TG");
        Employee employee1 = new Employee("John","Smith", 39, 1250.50, 3456, "Permanent", address);
        Employee employee2 = new Employee("Mike","Donovan", 59, 1950.50, 1456, "Contractor", address1);
        Client client1 = new Client("Jack", "Milt", 40, address2);
        System.out.printf("%s\n %s\n %s\n",employee1,employee2,client1);
    }
}

The employees and clients object contains an address as well so no need to include that inside the print statement.
Then in Employee I have the Address object passed to it which in turns gets passed to Person so it can get printed:

public Employee(String name, String surname, int age, double salary, int id, String role, Address address){     
        super(name,surname,age, address);
        ...
       @Override
public String toString(){
    return "\nEmployee " + super.toString() + " \nsalary = " + getSalary() + "\nID is " + identification.getID() + " \nRole is " + identification.getRole();
} 
        ...

And Person

...
    public Person(String name, String surname, int age, Address address){
    ...
    @Override
    public String toString(){           
        return String.format("\nName: %s,\nsurname: %s,\nage: %d, \naddress details: %s ", getName(), getSurname(), getAge(), address.toString());
    }

Trouble is it's returning a NullPointerException, but surely the Address object should be initialized OK by now

Violet_82 89 Posting Whiz in Training

OK let me try this then, thanks

Violet_82 89 Posting Whiz in Training

OK, makes sense for the toString, although my Address class isn't extending anything, it's just using composition. I've added a toString method in my Address class like so:

@Override
    public String toString(){
        return String.format("\nStreet number: %d, \nStreet name: %s, \nPostcode: %s ", getStreetNumber(), getStreetName(), getPostcode());     
    }

so that it knows how to print itself and then, in the Person class since I create an object of type Address somewhere I can reference that in the Person's toString like so:

@Override
    public String toString(){       
        return String.format("\nName: %s,\nsurname: %s,\nage: %d, \naddress details: %s ", getName(), getSurname(), getAge(), address.toString());
    }

Which means that I don't need to reference each field of Address like before as you suggested.
About the constructor, I understand what you mean, but how do I pass an object containing all the Address details? What I mean is,currently inside TestPerson I create objects of type Employee and Client passing the relevant address details to it

...
Employee employee1 = new Employee("John","Smith", 39, 1250.50, 3456, "Permanent", 34, "Flinch Street","KT25AG");
Employee employee2 = new Employee("Mike","Donovan", 59, 1950.50, 1456, "Contractor", 44, "London Road", "SW17FG");
Client client1 = new Client("Jack", "Milt", 40, 356, "Dean Street", "SW94TG");
...

These in turn calls the relevant constructors (Employee and Client) which then call the parent constructor (Person) to initialize the members of the Address class. I appreciate that Person shouldn't be the one creating the Address, but if I don't create it there I will have to create it where, TestPerson?
cheers

Violet_82 89 Posting Whiz in Training

OK, so would it be wrong to have the address info referenced from the Person class instead? The reason why I'm asking is because in Person I have a new Address object anyway, so I can print the address information directly from the Person class, here is the relevant code

public abstract class Person
{
    private String name;
    private String surname;
    private int age;
    private Address address;    
    public Person(String name, String surname, int age, int streetNumber, String streetName, String postcode){
        this.name = name;
        this.surname = surname;
        this.age = age;
        address = new Address(streetNumber,streetName,postcode);
    }
    ...
    @Override
    public String toString(){
        return String.format("\nName: %s,\nsurname: %s,\nage: %d, \nStreet number: %d, \nStreet name: %s, \nPostcode: %s ", getName(), getSurname(), getAge(),address.getStreetNumber(),address.getStreetName(),address.getPostcode());
    }

or is it more correct to print the address info from the address class as you said in your previous reply?
Violet_82 89 Posting Whiz in Training

Sure yeah, will do that, but if I then want to print the address I can't do it from the abstract class because that class calls the constructor of Address, so presumably I have to implement ToString in Address and call it from there instead...I believe
EDIT: no, that's a lie, I can access the getters of Address from the abstract class because I have a reference to address, so that's fine!

Violet_82 89 Posting Whiz in Training

That's OK. So one thing, if I create an Address class right, considering that both Client and Employee will use it, where would it be the best place to implement it? I was thinking that I could create an object of type Address inside Client and EMployee but that seems like a bit of a repetition. The thing is, I don't believe I can create an object in the abstract class Person though, so perhaps creating two Address objects, one for the Employee and one for the Client class is right

Violet_82 89 Posting Whiz in Training

eh eh, too late, already implemented it : -)! I see what you mean though, every employee actually has those attributes...Might leave it there for the time being but with the intention of removing it and incorporating the members inside the Employee class.
Another thing I've done was to correctly implement toString and remove that printStatus() method because, as you correctly pointed out, it was useless. Now I print objects directly from the test class like so:
System.out.printf("%s\n %s\n %s\n",employee1,employee2,client1);
I'll implement the project class as it sounds like a good idea and a better demonstration of the has-a relationship. So what should new class have? I'm thinking a project name of course, perhaps a method that shows whether that project has been assigned, so probably a boolean method that returns true or false somehow, how about that?

Violet_82 89 Posting Whiz in Training

Yes good points actually. So I've removed the salary from the parent class and added it to the employees only. Before getting to the multiple instances though, I thought it'd be nice to use composition, so perhaps I can use another class like Identification, which has an id number and a status, like permanent or contractor. This would only apply to Employees of course. After that I can implement the collection bit.
So, let's see how I can go about this. I'll create the class Identification - for the lack of a better name - and then presumably the Employee's constructor will have to take care of the initialization details, for instance, initializing the ID and the status.
In the meanwhile, here is the current amended code in its entirety:
Person.java

public abstract class Person
{
    private String name;
    private String surname;
    private int age;    
    public void printStatus(){
        System.out.println(this);
    }
    public Person(String name, String surname, int age){
        this.name = name;
        this.surname = surname;
        this.age = age;
        //this.salary = salary;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setSurname(String surname){
        this.surname = surname;
    }
    public void setAge(int age){
        this.age = age;
    }

    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public int getAge(){
        return age;
    }
    @Override
    public String toString(){
        return String.format("\nName: %s,\nsurname: %s,\nage: %d ", getName(), getSurname(), getAge());
    }
}

Person.java

public class Employee extends Person
{
    private double salary;
    public Employee(String name, String surname, int age, double salary){
        super(name,surname,age); …
Violet_82 89 Posting Whiz in Training

quick question as I'm working with the UI. Like, the way I'm proceeding is to get the console to print ot messages when I click the submit, reset or radio button (km to miles etc).
Now, this is all in the GUI file but I was planning to replace those console statements with function calls, not sure whether those function definitions will sit inside the UnitDef class or inside the GUI one. What do you think?

Violet_82 89 Posting Whiz in Training

Right, changes made. The abstract class now has a new member, salary and the old printStatus is not abstract anymore but I've implemented it in there. I've also added setters and getters for the salary

public abstract class Person
{
    private String name;
    private String surname;
    private int age;
    private double salary;
    //abstract void printStatus();
    public void printStatus(){
        System.out.println(this);
    }
    public Person(String name, String surname, int age, double salary){
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.salary = salary;
    }
    @Override
public String toString(){
    return String.format("\nName: %s,\nsurname: %s,\nage: %d,\nsalary: %.2f\n ", getName(), getSurname(), getAge(), getSalary());
}   
    ...

The two extended classes are now smaller

public class Employee extends Person
{
    public Employee(String name, String surname, int age, double salary){
        super(name,surname,age,salary);
        System.out.print("I'm an Employee. ");
    }
}

and

public class Client extends Person
{
    public Client(String name, String surname, int age, double salary){
        super(name,surname,age,salary);
        System.out.print("I'm an Client. ");
    }
    }

The test class is the same.
Say I wanted to have quite a few instances of EMployees and Client, what would be the best way forward? Don't know I'm just trying to think about what it could be added to it for me to practice a little bit more with inheritance, polymorphism and relationships among classes in general

Violet_82 89 Posting Whiz in Training

Thanks for the feedback. To be fair the printStatus method is there only because I wanted to have to implement an abstract method, and I couldn't think of anything else :-). I usually go back to my classes after a long time and I thought it'd be nice to have an abstract method there, if I will have another method later on - and I'm sure I will - I can make that one abstract and get rid of printStatus altogether, but yes I see your point, I could call toString directly without going through printStatus.
I will add a few more properties to the class and then post back

Violet_82 89 Posting Whiz in Training

Right, so here is the code, it's just a simple application creating one Employee and one Client object and print out the result.
Obviously if there is anything you think isn't right, let me know and I will amend.
What do you reckon would be a good thing to do with this code to, say, enhance it? Perhaps get a list of Clients and/or Employees?

Person.java

public abstract class Person
{
    private String name;
    private String surname;
    private int age;
    abstract void printStatus();
    public Person(String name, String surname, int age){
        this.name = name;
        this.surname = surname;
        this.age = age;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setSurname(String surname){
        this.surname = surname;
    }
    public void setAge(int age){
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public int getAge(){
        return age;
    }
    @Override
    public String toString(){
        return String.format("name: %s, surname: %s, age: %d ", getName(), getSurname(), getAge());
    }
}

Employee.java

public class Employee extends Person
{
    public Employee(String name, String surname, int age){
        super(name,surname,age);
    }

    @Override
    void printStatus()
    {
        System.out.println("I'm an Employee " + super.toString());

    }
}

Client.java

public class Client extends Person
{
    public Client(String name, String surname, int age){
        super(name,surname,age);
    }
    @Override
    void printStatus()
    {
        System.out.println("I'm a client: " + super.toString());

    }
}

TestPerson.java

public class TestPerson
{

    public static void main(String[] args)
    {
        //create objects of type person
        Employee employee1 = new Employee("John","Smith",39);
        employee1.printStatus();
        Client client1 = new Client("Jack", "Milt", 40);
        client1.printStatus();
    }

}

A …

Violet_82 89 Posting Whiz in Training

OK no worries.
I've also tried a new conversion and it works:

/*weights*/        
       UnitDef grams = new UnitDef("g");//base unit 
        UnitDef Kilo = new UnitDef("K",1000);
        UnitDef pound = new UnitDef("pound", 453.593); //pounds in 1 g
        UnitDef oz = new UnitDef("Oz", 28.349);              
        double kilograms = 5.0;
        double baseValue = Kilo.convertToBaseValue(kilograms);
        double pounds = pound.convertFromBaseValue(baseValue, "pound");
        /*weights*/

5kg returns 11.023097799128292 pound (I might round it up a bit, but maybe later).
So, now, if I think about the GUI (providing it's not too early of course) what would be the best way forward? Like having radio buttons as before, like km to miles, etc etc?
Also, in terms of classes, I have only UnitDef.java with the logic and the MyUI.java for the GUI: is that OK?

Violet_82 89 Posting Whiz in Training

Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty to work on, rather do small exercises to help me understand the concept.
So, I was thinking, maybe I can create a small program that output the characteristics of either an employee or a client of an organisation, perhaps loop through them, just to better understand relationships between classes.
So, while I’m open to suggestions of course, here is the plan:
-create an abstract class Person, that has at least a name and a surname, perhaps a method returning a string
-create a class Employee that implements Person and that perhaps contains its own method (not sure which one at the moment, maybe something that prints "I'm a en employee")
-create a class Client that implements Person and that perhaps contains its own method (not sure which one at the moment, maybe something that prints "I'm a client")
How does that sound as a start?

Violet_82 89 Posting Whiz in Training

that's fine, I'll try a few more conversions first.

but seeing variables public without final always rings an alarm bell for me.

Are you talking about these variables?

UnitDef Meters = new UnitDef("M");//base value constructor
        UnitDef kM = new UnitDef("kM",1000);//KM conversion
        UnitDef mile = new UnitDef("Mile", 1609.34);
        UnitDef yard = new UnitDef("yards", 0.9144);

As a side note, it might be that I will have to redo the whole thing again, some time soon, as there is the possibility that I could be using the same brief as practice at work, but that hasn't been decided yet. If that happens I will have to do it again, and of course I won't be using this code but I have to come up with something else... That's because I'm looking fo things to build at work as exercise you seem but will see what happens

Violet_82 89 Posting Whiz in Training

Right, sorry I've been away for a while.
I've now made some changes and it seems that the conversion works correctly. Here is the code:
Test code in MyUI.java

UnitDef Meters = new UnitDef("M");//base value constructor
        UnitDef kM = new UnitDef("kM",1000);//KM conversion
        UnitDef mile = new UnitDef("Mile", 1609.34);
       /* UnitDef yard = new UnitDef("yards", 0.9144);*/

        double kMs = 5.0;
        double baseValue = kM.convertToBaseValue(kMs);
        double miles = mile.convertFromBaseValue(baseValue);
       /* double yards = yard.convertFromBaseValue(baseValue);*/

And the UnitDef.java

package com.vaadin.project.converterRedone;

import com.vaadin.ui.CustomComponent;

public class UnitDef extends CustomComponent{
    public String name;
    public double multiplier;
    public UnitDef(String name){//base unit
        this.name = name;
        this.multiplier = 1;
    }
    public UnitDef(String name, double multiplier){//overloaded constructor taking name and multiplier depending on conversion
        this.name = name;
        this.multiplier = multiplier;
    }
    public double convertToBaseValue(double value){
        value = value * multiplier;
        System.out.println("convertToBaseValue() and value is " + value);
        return value;

    }
    public double convertFromBaseValue(double value){
        value = value / multiplier;
        System.out.println("convertFromBaseValue() and value is " + value);
        return value;

    }
}

Can I start thinking about the GUI or should we implement all the possible conversions?

Violet_82 89 Posting Whiz in Training

OK fab, thanks for the advices

Violet_82 89 Posting Whiz in Training

Cool, but does the notion that you shouldn't let the battery discharge below 40/50% and then connect it to the main still hold true?

Violet_82 89 Posting Whiz in Training

Right, so in my test class (MyUI) I have the object creation. For now, I only create KM and metres:

UnitDef Meters = new UnitDef("M");//base value constructor
        UnitDef kM = new UnitDef("kM",1000);//KM conversion
        /*UnitDef mile = new UnitDef("Mile", 1609.34);
        UnitDef yard = new UnitDef("yards", 0.9144);*/

        double kMs = 5.0;
        double baseValue = kM.convertToBaseValue(kMs);
        /*double miles = mile.convertFromBaseValue(baseValue);
        double yards = yard.convertFromBaseValue(baseValue);*/

And then the UnitDef

public class UnitDef extends CustomComponent{
    public  String name;
    public  double multiplier;
    public UnitDef(String name){//base unit
        this.name = name;
        this.multiplier = 1;
    }
    public UnitDef(String name, double multiplier){//overloaded constructor taking name and multiplier depending on conversion
        this.name = name;
        this.multiplier = multiplier;
    }
    public double convertToBaseValue(double value){
        value = value / multiplier;
        System.out.println("convertToBaseValue() and value is " + value);
        return value;

    }
    public double convertFromBaseValue(double value){
        value = value * multiplier;
        System.out.println("convertFromBaseValue() and value is " + value);
        return value;

    }
}

Now,this is supposed to test only KM. I assigned a value of 5 to km, but the conversion to base is obviously off, as it gives me 0,005 rather than 5000, so should I define KM as 1/5 instead?

Violet_82 89 Posting Whiz in Training

OK thanks, sorry I didn't realize that was your own class.
I think the benefits of this approach will become obvious to me later on, and I also think I will have a lot of questions to ask, some perhaps you might think will be silly.

So, let me pause for a second and think about what to do.
To start with you're saying to have a class similar to your UnitDefinition that represent the conversion and that therefore does all the conversion using the base reference "metres": in this class no matter what conversion you want to do, everything gets converted first to metres and then to the unit you want to convert to, like yards to metres to km. What if I want to convert to metres instead?
Then create another class to test that class. But how do I get that to work with a GUI interface, like radio buttons? Or shouldn't I worry about that as yet?

Violet_82 89 Posting Whiz in Training

OK, so I had a look at the UnitDefinition, but I'm not sure how that works. I looked for some tutorial but I couldn't find any, strangely I couldn't even find it in the Java API. I was looking for it so I could read how that works etc, I mean, if you say that's the easiest way then I'm happy to try

Violet_82 89 Posting Whiz in Training

It's an Asus UX305CA - FB005T

Violet_82 89 Posting Whiz in Training

Right, so I'll switch it off and then on again when I need it.
Is it good practice to then connect to the main the laptop again when the battery reaches 40-50%? Some say that it is better for the battery itself. One more thing, this laptop is not the main one, so it will be likely to be off for most of the week. I'm not into that kind of do-everything-you-can-to-save-the-battery person, but still, if there are simply things I can do to make sure that my battery lasts as long as I can I'm happy to do it. This laptop doesn't have a removable battery though, hopefully it will be OK. In the meanwhile, I will set the battery to balanced mode too, as it is on high performance now

Violet_82 89 Posting Whiz in Training

So my new Asus UX305 has just arrived. I charged the battery till full and I'm now using the laptop. I'm planning to switch it off very soon, and then it occurred to me, do I have to break in the battery, or can I just use it as normal (meaning use it till it reaches 40-50% and then plug it in?). Just to clarify, by "break in" they mean like run a full discharge and then recharge cycle at least 3-4 times
I looked up on the net about breaking in the battery but there isn't too much from authoritative sources
Any advice is of course appreciated
thanks

Violet_82 89 Posting Whiz in Training

Right, so I tried to do what you suggested and this is how I did it.
You said to think about the methods that use that class.
So, first of all a constructor, which will initialize the conversion:

public ConverterModel(double unitToConvert){        
        this.unitToConvert = unitToConvert;
    }

unitToConvert should contain a double which is the number to convert. This eventually will come from user input
I'm not initializing the multipliers because I thought those will be constants, something like:

private final double KM_TO_MILES = 0.62137; //multiplication
private final double MILES_TO_KM = 1.6; //multiplication
private final double METRES_TO_YARDS =  0.9144; //division

Anyway, back to the methods.
A setter and getter for unitToConvert.

I will then need to do the actual conversion.
The signature method should be something like this
private void doConversion(double unitToConvert);
so it takes only the unit to convert
Now, I need a way to work out which constant to use, and the only way I'm thinking to do that is to make some checks inside the conversion method, like, if unit is KM then use KM_TO_MILES etc, perhaps a switch statement
Also, I think I wasn't clear on the multiplication vs division earlier on. What I meant was, even if I can multiply by the conversion constant or multiply by 1/conversion constant, I still need a mechanism to determine which one I need to do one or the other